Reputation: 3113
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. :(
Upvotes: 0
Views: 1859
Reputation: 43974
Have you looked at using XmlSerializer
? Create a class to hold all your data, create an instance of your class and then use XmlSerializer to write it out to an XML file.
Upvotes: 0
Reputation: 57210
Have you tried something like this ?
// add the root node
xmlWriter.WriteStartElement("client");
// add the attribute to root node
xmlWriter.WriteStartAttribute("foo");
// add the value of the attribute
xmlWriter.WriteValue("attribute value...");
// close the attribute to root node
xmlWriter.WriteEndAttribute();
// close the root node
xmlWriter.WriteEndElement();
Upvotes: 1
Reputation: 9986
If you are using 3.5 or higher, XDocument
would make you fall in love.
Upvotes: 3