XMl Document Object Help

Hi iam new to XML.I want the sample source code of xml document for creating nodes,elements,attributes and reading the same.I have gone through msdn and got a few but its confusing.It would be great if anybody has it.

thanks,

manoj




Answer this question

XMl Document Object Help

  • streetlightman

    thanks Martin

  • QuantumMischief

    If you want to create an XML document then you can use XmlWriter e.g.

    XmlWriterSettings writerSettings = new XmlWriterSettings();
    writerSettings.Indent = true;
    string fileName = @"file.xml";
    using (XmlWriter writer = XmlWriter.Create(fileName, writerSettings)) {
    writer.WriteStartDocument();
    writer.WriteStartElement("gods");
    writer.WriteElementString("god", "Kibo");
    writer.WriteElementString("god", "Xibo");
    writer.WriteEndElement();
    writer.WriteEndDocument();
    writer.Close();
    }

    creates e.g.

    < xml version="1.0" encoding="utf-8" >
    <gods>
    <god>Kibo</god>
    <god>Xibo</god>
    </gods>

    Once you have an XML file and you want to further manipulate it you can use XmlDocument to load the XML document into a DOM tree, manipulate the tree and save the tree back e.g.

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(fileName);
    XmlElement god = xmlDocument.CreateElement("god");
    god.InnerText = "Jaffo";
    xmlDocument.DocumentElement.AppendChild(god);
    xmlDocument.Save(fileName);



  • XMl Document Object Help