I have the following code:
string PathName = "C:\\Users\\TestUser\\Documents\\Project";
string FileName = "settings.xml";
XmlDocument Settings = new XmlDocument();
Settings.Load(Path.Combine(PathName, FileName));
XmlNode KnowledgeNode = Settings.SelectSingleNode("/Config/Map/Knowledge");
XmlNode UsersNode = Settings.CreateNode(XmlNodeType.Element, "Users", null);
XmlAttribute FileDirectory = Settings.CreateAttribute("FileDirectory");
FileDirectory.Value = UserSelectedFileDirectory;
UsersNode.Attributes.Append(FileDirectory);
KnowledgeNode.AppendChild(UsersNode);
Settings.Save(Path.Combine(PathName, FileName));
This results in my XML file containing <Users FileDirectory="C:\data" />
instead of <Users FileDirectory="C:\data" ></Users>
as I want.
How do I create the end element? I've given it a Google and I can't find much. Any help is appreciated, thanks.
Here are three ways to force XmlDocument.Save to output a separate end tag for an empty XmlElement instead of an empty, self-closing tag.
Insert an empty whitespace node inside the element:
UsersNode.AppendChild(Settings.CreateWhitespace(""));
Here's the output:
<Users FileDirectory="C:\data"></Users>
Set the XmlElement.IsEmpty property of the element to false:
((XmlElement)UsersNode).IsEmpty = false;
Note that with this method, the default XML formatting settings will insert a line break between the start tag and the end tag. Here's the output:
<Users FileDirectory="C:\data">
</Users>
Derive a custom XmlTextWriter that forwards all WriteEndElement calls to WriteFullEndElement:
public class CustomXmlTextWriter : XmlTextWriter
{
public CustomXmlTextWriter(string fileName)
: base(fileName, Encoding.UTF8)
{
this.Formatting = Formatting.Indented;
}
public override void WriteEndElement()
{
this.WriteFullEndElement();
}
}
Usage:
using (var writer = new CustomXmlTextWriter(Path.Combine(PathName, FileName)))
{
Settings.Save(writer);
}
This method might require less code overall if you have a lot of empty elements in your document.
As with Method 2, the default XML formatting settings will insert a line break between the start tag and end tag of each empty element.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With