A reasonable way to add attributes to an xml root element in C#.

Posted by DrLazer on Stack Overflow See other posts from Stack Overflow or by DrLazer
Published on 2010-04-15T15:20:27Z Indexed on 2010/04/15 15:33 UTC
Read the original article Hit count: 259

Filed under:
|

The function "WriteStartElement" does not return anything. I find this a little bizzare. So up until now I have been doing it like this.

XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(m_targetFilePath, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("client");
xmlWriter.Close();
xmlDoc.Load(m_targetFilePath);
XmlElement root = xmlDoc.DocumentElement;

Saving the doc, then reloading it to get hold of the start element so i can write attributes to it. Does anybody know the correct way of doing this because I'm pretty sure what I'm doing isn't right.

I tried to use xmlWriter.AppendChild() but it doesnt seem to write out anything. :(

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml