none
Add a node to child node

    Question

  • My XML is

    <?xml version="1.0" encoding="utf-8"?>
    <Books>
      <Book Name="ASP.NET">
        <Publish Year="2013">
          <Version>Version 3</Version>
        </Publish >
      </Book>
       <Book Name="C#.NET">
        <Publish Year="2013">
          <Version>Version 3</Version>
        </Publish >
      </Book>
    </Books>

    in the publish tag i want to add another version tag.

    i have written the code as below:

    string Name= "", Year = "";
                    XmlElement elmRoot = xmlDoc.DocumentElement;
                    XmlElement elmNew1 = xmlDoc.CreateElement("Book");
                    XmlAttribute att = xmlDoc.CreateAttribute("Name");
                    XmlElement elmNew2;
                    
                    //Loading the XML
                    xmlDoc.Load(strFilename);
                    Acronym = "/Books/Book[@Name='" + textBox1.Text + "']";
                    XmlNodeList xnLista = xmlDoc.SelectNodes(Name);
                    if (xnLista != null)
                    {
                        Year = "/Books/Book/Publish[@Year='" + textBox2.Text + "']";
                        //XmlNode xmlnode = xmlDoc.SelectSingleNode(Year);
                        XmlNodeList xmlnode = xmlDoc.SelectNodes(Year);
                        if (xmlnode != null)
                        {
                            elmRoot = xmlDoc.DocumentElement;
                            
                            XmlElement elmNew3 = xmlDoc.CreateElement("Document");
                            XmlText txtVideo = xmlDoc.CreateTextNode(textBox3.Text);
                            string attribute = "";
                            
                            using (XmlReader reader = XmlReader.Create("Books.xml"))
                            {
                                while (reader.Read())
                                {
                                    if (reader.NodeType == XmlNodeType.Element)
                                    //if (reader.IsStartElement())
                                    {
                                        switch (reader.Name)
                                        {
                                            case "Publish":
                                                xmlnode.AppendChild(elmNew3);
                                                xmlnode.LastChild.AppendChild(txtVideo);
                                                break;
                                        }
                                    }
                                }
                            }
                        }

    with this code i am not able to insert, please suggest me where to change the code 

    Thanks 

    Kiran Kumar


    Thursday, October 10, 2013 12:42 PM

Answers

  • Hello,

    Welcome to this forum.

    From your description, for achieving what you want, my suggestion is that we can use LinqToXML. It is very simple to operate the XML file.

    And here I made a sample and please see it below:

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Threading.Tasks;
    
    using System.Xml;
    
    using System.Xml.Linq;
    
    using System.Xml.XPath;
    
    namespace LinqToXml
    
    {
    
        class OperateXMLUsingLinq
    
        {
    
            public void Execute()
    
            {
    
                string xmlFile = "E:\\BMX\\Lab\\SmapleFile\\2013-10\\Sample_11.xml";
    
    
                string message = string.Empty;
    
    
                XDocument doc = XDocument.Load(xmlFile);
    
    
                foreach (XElement Book in doc.Element("Books").Elements("Book"))
    
                {
    
                    foreach (XElement Publish in Book.Elements("Publish"))
    
                    {
    
                        Publish.Element("Version").AddAfterSelf(new XElement("Version", "Version 4"));
    
                    }
    
                }
    
    
                //doc.XPathSelectElement("Books/Book/Publish/Version").AddAfterSelf(new XElement("Version", "Version 4"));
    
    
                doc.Save(xmlFile);
    
            }
    
        }
    
    }
    

    The result:

    <?xml version="1.0" encoding="utf-8"?>
    
    <Books>
    
      <Book Name="ASP.NET">
    
        <Publish Year="2013">
    
          <Version>Version 3</Version>
    
          <Version>Version 4</Version>
    
        </Publish>
    
      </Book>
    
      <Book Name="C#.NET">
    
        <Publish Year="2013">
    
          <Version>Version 3</Version>
    
          <Version>Version 4</Version>
    
        </Publish>
    
      </Book>
    
    </Books>
    

    If this does not work for you, please let know.

    More information about LinqToXML:

    http://msdn.microsoft.com/en-us/library/bb387098.aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 11, 2013 3:10 AM