XmlTextWriter.WriteFullEndElement tags on the same line
I am using an XMLTextWriter to create an XML document dynamically (in VB.Net).
I want empty 开发者_Go百科tags to appear like this - <Tag></Tag>
and not this - <Tag />
So, I am using WriteFullEndElement to end the element tag. But it is writing out the tag as -
<Tag>
</Tag>
i.e. with a newline character between the tags. The web service reading this XML rejects it due to the newline character.
How do I avoid the newline, and have both the start and end tags on the same line?
Use XmlWriterSettings
.
Specifically, use the NewLineHandling and NewLineChars.
Following is a simple example of writing XML document in c#:
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.Indent = true;
writerSettings.NewLineHandling = NewLineHandling.None;
using (XmlWriter textWriter = XmlTextWriter.Create(this.fileName, writerSettings))
{
textWriter.WriteStartDocument();
textWriter.WriteComment("generated file");
textWriter.WriteStartElement("Root");
// Populate data
foreach (object o in someList)
{
textWriter.WriteStartElement(o);
textWriter.WriteString(o);
textWriter.WriteFullEndElement ();
}
textWriter.WriteFullEndElement ();
textWriter.WriteEndDocument();
textWriter.Close();
}
Cheers
精彩评论