tags:

views:

114

answers:

3

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. :(

+3  A: 

If you are using 3.5 or higher, XDocument would make you fall in love.

KMan
only just moved onto 3.5 - i think im about to convert my class
DrLazer
A: 

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();
digEmAll
A: 

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.

Barry