You must Sign In to post a response.
  • Category: XML

    How to Create and Append XML Node ?


    Are you looking for a way to Create and Append XML Node ? then read this thread to know more about it



    Hi,

    create and Append XML Node


    public static void Main() {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
    "<title>Pride And Prejudice</title>" +
    "</book>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.AppendChild(elem);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);

    }

    Regards
  • #744570
    Refer this sample file for creating a XML

    public void createXML()
    {
    XmlDocument doc = new XmlDocument();
    string filename="c:\\sample.xml";
    XmlTextWriter tw=new XmlTextWriter(filename,null);
    tw.Formatting=Formatting.Indented;
    tw.WriteStartElement("Author");
    tw.WriteElementString("ID","1");
    tw.WriteElementString("SubmissionDate","1-1-08");
    tw.WriteElementString("Topic","test");
    tw.WriteElementString("Title","test");
    tw.WriteStartElement("Authors");
    tw.WriteStartElement("Author");
    tw.WriteElementString("Name","XYZ");
    tw.WriteElementString("Affiliation","ABC");
    tw.WriteEndElement();
    tw.WriteEndElement();
    tw.WriteElementString("Summary","Hi");
    tw.WriteElementString("Text","Hello");
    tw.WriteEndElement();
    tw.Flush();
    tw.Close();
    StreamReader srXml = new StreamReader(filename);
    string xmlFile = srXml.ReadToEnd()
    }

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #746114
    http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.appendchild.aspx

  • #746540
    Hai Dipti,
    To create a new node and append it to the child element of the existing XML nodes, you can use the below code snippet:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xmlFileString);
    XmlNode root = doc.DocumentElement;
    XmlElement element = doc.CreateElement("TestNode");
    element.InnerText="test";
    root.AppendChild(element);
    doc.Save(Console.Out);

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • Sign In to post your comments