views:

49

answers:

3

Hi Guys and Girls,

Its been a while since i've needed to do this so i was looking at the old school methods of writing XMLDocument from code down to a File.

In my application i am writing alot to an XMLdocument with new elements and values and periodically saving it down to disc and also reading from the file and depending on the data i am doing things.

I am using methods like File.Exists(...) _xmldoc.LoadFile(..) etc...

Im wondering probably now a days there are better methods for this with regards

  • Parsing the XML
  • Checking its format for saving down
  • rather then the data being saved down being treated as text but as XML

maybe what i am doing is fine but its been a while and wondered if there are other methods :)

thanks

+2  A: 

Well there's LINQ to XML which is a really nice XML API introduced in .NET 3.5. I don't think the existing XMLDocument API has changed much, beyond some nicer ways of creating XmlReaders and XmlWriters (XmlReader.Create/XmlWriter.Create).

I'm not sure what you really mean by your second and third bullets though. What exactly are you doing in code which feels awkward?

Jon Skeet
+1  A: 

Have you looked at the Save method of your XmlDocument? It will save whatever is in your XmlDocument as a valid formatted file.

If your program is able to use the XmlDocument class, the XmlDocument class will be able to save your file. You won't need to worry about validating before saving, and you can give it whatever file extension you want. As to your third point... an XML file is really just a text file. It won't matter how the OS sees it.

Brad Bruce
+1  A: 

I was a big fan of XmlDocument due to its facility to use but recently I got a huge memory problem with that class so I started to use XmlReader and XmlWriter.

XmlReader can be a little bit tricky to use if your Xml file is complex because you read the Xml file sequentially. In that case, the method ReadSubTree of XmlReader can be very useful because this method returns only the xml tree under the current node so you send the new xmlreader to a function to parse the subnode content and once it is done, you continue to the next node.

XmlReader Example:

string xmlcontent = "<BigXml/>";

using(StringReader strContent = new StringReader(xmlcontent))
{
    using (XmlReader reader = XmlReader.Create(strContent))
    {
        while (reader.Read())
        {
            if (reader.Name == "SomeName" && reader.NodeType == XmlNodeType.Element)
            {
                //Send the XmlReader created by ReadSubTree to a function to read it.
                ReadSubContentOfSomeName(reader.ReadSubtree());
            }
        }
    }
}

XmlWriter Example:

StringBuilder builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder))
{
    writer.WriteStartDocument();

    writer.WriteStartElement("BigXml");
    writer.WriteAttributeString("someAttribute", "42");
    writer.WriteString("Some Inner Text");

    //Write nodes under BigXml
    writer.WriteStartElement("SomeName");
    writer.WriteEndElement();

    writer.WriteEndElement();

    writer.WriteEndDocument();
}
Francis B.