渲染空的XML元素的父元素 [英] Render Empty XML Elements as Parent Elements
问题描述
我在那里消耗一些XML我的应用程序实际上是产生一个应用程序需要被序列化为父元素空元素一个奇怪的要求。例如:
I have a strange requirement where an application consuming some XML that my application is generating actually needs empty elements to be serialized as parent elements. For example:
<element foo="bar" />
应该是:
<element foo="bar"></element>
我不知道有任何方式的的 XmlSerializer的可以让你改变这种情况。是否有人知道如何做到这一点?
I'm not aware of any way that the XmlSerializer allows you to change this. Does anybody know how to accomplish this?
推荐答案
我伸出的 XmlTextWriter的这样我就可以覆盖对writeEndElement()方法,迫使它来调用WriteFullEndElement()。这样做有问题。
I extended XmlTextWriter so that I could override the WriteEndElement() method, forcing it to call WriteFullEndElement(). This did the trick.
注意:任何人,看到我的问题更新,请忽略。 IE被渲染的简写形式的XML。 。当我在记事本打开它,我意识到一切工作正常。
Note: for anybody that saw my question update, please ignore. IE was rendering the XML in the shorthand form. As soon as I opened it in Notepad, I realized everything was working fine.
public class FullEndingXmlTextWriter : XmlTextWriter
{
public FullEndingXmlTextWriter(TextWriter w)
: base(w)
{
}
public FullEndingXmlTextWriter(Stream w, Encoding encoding)
: base(w, encoding)
{
}
public FullEndingXmlTextWriter(string fileName, Encoding encoding)
: base(fileName, encoding)
{
}
public override void WriteEndElement()
{
this.WriteFullEndElement();
}
}
这篇关于渲染空的XML元素的父元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!