none
Add Node existing XML file

    Question

  • - <RadioBar>
      <Genre_Genre_Country>Genre_Country</Genre_Genre_Country>
    - <Genre_Genre_Country>
      <Title>KPIG presents Cowboy Cultural Society Radio - an American tradition since 2002</Title>
      <url>http://205.188.215.232:8028</url>
      </Genre_Genre_Country>
    - <Genre_Genre_Country>
      <Title>Classic Heartland - Classic Country, Western, Bluegrass, Alternative Country [Dial-up]</Title>
      <url>http://130.166.72.1:8008</url>
      </Genre_Genre_Country>
     Genre_Genre_Favorites>Genre_Favorites</Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>KPIG presents Cowboy Cultural Society Radio - an American tradition since 2002</Title>
      <url>http://205.188.215.232:8028</url>
      </Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>Classic Heartland - Classic Country, Western, Bluegrass, Alternative Country [Dial-up]</Title>
      <url>http://130.166.72.1:8008</url>
      </Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>ROOSTER CLASSIC COUNTRY PLAYING 50 YEARS OF GREAT COUNTRY MUSIC! ROOSTERRADIO.US</Title>
      <url>http://72.236.125.114:8020</url>
      </Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>Americana Roots Radio :: Americana, Texas Music, Alt-Country, Red Dirt</Title>
      <url>http://72.36.236.154:8000</url>
      </Genre_Genre_Favorites>
      </RadioBar>
     
    How can I add a node with Title and url into Favorites or Country? Sorry, XML....gives me a large headache.
       
    Wednesday, August 23, 2006 5:33 AM

Answers

  • here is modified code that append following to the end of file

    //file name
    string filename = @"d:\temp\XMLFile2.xml";

    //create new instance of XmlDocument
    XmlDocument doc = new XmlDocument();

    //load from file
    doc.Load(filename);

    //create node and add value
    XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
    //node.InnerText = "this is new node";

    //create title node
    XmlNode nodeTitle = doc.CreateElement("Title");
    //add value for it
    nodeTitle.InnerText = "This title is created by code";

    //create Url node
    XmlNode nodeUrl = doc.CreateElement("Url");
    nodeUrl.InnerText = @"http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&SiteID=1";

    //add to parent node
    node.AppendChild(nodeTitle);
    node.AppendChild(nodeUrl);

    //add to elements collection
    doc.DocumentElement.AppendChild(node);

    //save back
    doc.Save(filename);


    and to the end is appended

    <Genre_Genre_Country>
    <Title>This title is created by code</Title> <Url>http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&amp;SiteID=1</Url>
    </Genre_Genre_Country>


    Take a look at the code. You can change nodes at whatever you want as they are presented as string

    Hope this helps
    Thursday, August 24, 2006 8:29 AM

All replies

  • Hi. I think is what you looking for


    //file name
                string filename = @"d:\temp\XMLFile2.xml";

                //create new instance of XmlDocument
                XmlDocument doc = new XmlDocument();

                //load from file
                doc.Load(filename);
     
                //create node and add value
                XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
                node.InnerText = "this is new node";

                //add to elements collection
                doc.DocumentElement.AppendChild(node);

                //save back
                doc.Save(filename);


    Hope this helps
    Wednesday, August 23, 2006 8:19 AM
  • Returns <Genre_Genre_Favorites>this is new node</Genre_Genre_Favorites>  at the end of the file.

    Is there any way to write it into the Favorites section?

    Wednesday, August 23, 2006 4:44 PM
  • sorry but I am not able to understand your question... where is the favorites section?

    What result do you want to achieve?

    Wednesday, August 23, 2006 5:56 PM
  • This  XML contains a Country and Favorites "section". There will potentially be 50 or so "sections.

    <Genre_Genre_Country>Genre_Country</Genre_Genre_Country>

     Genre_Genre_Favorites>Genre_Favorites</Genre_Genre_Favorites>

    <RadioBar>

      <Genre_Genre_Country>Genre_Country</Genre_Genre_Country>
    - <Genre_Genre_Country>
      <Title>KPIG presents Cowboy Cultural Society Radio - an American tradition since 2002</Title>
      <url>http://205.188.215.232:8028</url>
      </Genre_Genre_Country>
    - <Genre_Genre_Country>
      <Title>Classic Heartland - Classic Country, Western, Bluegrass, Alternative Country [Dial-up]</Title>
      <url>http://130.166.72.1:8008</url>
      </Genre_Genre_Country>
     Genre_Genre_Favorites>Genre_Favorites</Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>KPIG presents Cowboy Cultural Society Radio - an American tradition since 2002</Title>
      <url>http://205.188.215.232:8028</url>
      </Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>Classic Heartland - Classic Country, Western, Bluegrass, Alternative Country [Dial-up]</Title>
      <url>http://130.166.72.1:8008</url>
      </Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>ROOSTER CLASSIC COUNTRY PLAYING 50 YEARS OF GREAT COUNTRY MUSIC! ROOSTERRADIO.US</Title>
      <url>http://72.236.125.114:8020</url>
      </Genre_Genre_Favorites>
    - <Genre_Genre_Favorites>
      <Title>Americana Roots Radio :: Americana, Texas Music, Alt-Country, Red Dirt</Title>
      <url>http://72.36.236.154:8000</url>
      </Genre_Genre_Favorites>
      </RadioBar>
    If possible I would like to add to the section, rather than append to the end of file. Should make reading the file faster...as there could be thousands of Titles in the file.
     
    So I want to add :
    Title "This is the Title"
     
    to the Genre_Genre_Favorites>Genre_Favorites</Genre_Genre_Favorites> node it will have to look like this:
    <Genre_Genre_Favorites>
      <Title>Americana Roots Radio :: Americana, Texas Music, Alt-Country, Red Dirt</Title>
      <url>http://72.36.236.154:8000</url>
      </Genre_Genre_Favorites>
     
    Wednesday, August 23, 2006 6:14 PM
  • here is modified code that append following to the end of file

    //file name
    string filename = @"d:\temp\XMLFile2.xml";

    //create new instance of XmlDocument
    XmlDocument doc = new XmlDocument();

    //load from file
    doc.Load(filename);

    //create node and add value
    XmlNode node = doc.CreateNode(XmlNodeType.Element, "Genre_Genre_Country", null);
    //node.InnerText = "this is new node";

    //create title node
    XmlNode nodeTitle = doc.CreateElement("Title");
    //add value for it
    nodeTitle.InnerText = "This title is created by code";

    //create Url node
    XmlNode nodeUrl = doc.CreateElement("Url");
    nodeUrl.InnerText = @"http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&SiteID=1";

    //add to parent node
    node.AppendChild(nodeTitle);
    node.AppendChild(nodeUrl);

    //add to elements collection
    doc.DocumentElement.AppendChild(node);

    //save back
    doc.Save(filename);


    and to the end is appended

    <Genre_Genre_Country>
    <Title>This title is created by code</Title> <Url>http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=659406&amp;SiteID=1</Url>
    </Genre_Genre_Country>


    Take a look at the code. You can change nodes at whatever you want as they are presented as string

    Hope this helps
    Thursday, August 24, 2006 8:29 AM
  • Thanks again Galin....my hair is now growing back.
    Thursday, August 24, 2006 12:20 PM
  • yeah this help me lot but i was trying to ammend in rss2.0 file it was creating error

    Saturday, February 02, 2008 1:28 PM

  •         XmlDocument doc = new XmlDocument();
            doc.Load("C:\\abc.xml");
            XmlNodeList nodeList = doc.SelectNodes("//AmbiguousData/AmbiguousWords/Word");
            XmlNode node = doc.ChildNodes[4];

            XmlNode controlAttrNode = null;
            controlAttrNode = doc.CreateElement("Message");
            node.AppendChild(controlAttrNode);

            doc.Save("c:\\ac.xml");
    Saturday, February 09, 2008 3:47 PM
  • when i add a node with name of "geo:lat", there is only "lat" display in my xml file , why it is not full name "geo:lat", any suggestion will be appreciated!
    my code is below:

     

    Tuesday, August 05, 2008 7:39 AM
  • Please open your own thread on this, not reopen a 2 year old thread that has been solved aswell.

    Thank you.

    Ps: xml name spaces. (xmlns) is the answer to your question.
    The improbable we do, the impossible just takes a little longer. - Steven Parker
    Tuesday, August 05, 2008 8:36 AM
  • Hi buddy,
    Could you help me in this. I have an xml file with the content
    <?xml version="1.0" encoding="UTF-8"?>
    <COMPANY>
      <BALANCE>
        <REQUEST>
          <DATA>20082999</DATA>
          <OUTLET>1000</OUTLET>
    <OPTRID>9999</OPTRID>
          <DATE>08/09/2008</DATE>
          <TIME>11:58:02</TIME>
        </REQUEST>
      </BALANCE>
    </COMPANY>


    Now I have to insert an element <OPTRID> after outlet or anywere within <REQUEST>.

    Hope to hear from you soon.

    Regards
    Ansaf



    Smile a lot, it costs nothing
    Saturday, August 09, 2008 11:18 AM
  • Hey guys I got the answer :-)

    This is how to do

    XmlElement root = doc.DocumentElement;

                XmlNodeList nodeList = doc.GetElementsByTagName("OUTLET");

                foreach (XmlNode node in nodeList)
                {
                    //Can avoid this if there is only one occurrence in the XML.
                    if (node.OuterXml.Contains("OUTLET"))
                    {
                        XmlElement newElement = doc.CreateElement("OPTRID");
                        XmlText txtVal = doc.CreateTextNode("9999");
                        XmlNode parent1 = node.ParentNode;
                        parent1.AppendChild(newElement);
                        parent1.LastChild.AppendChild(txtVal);

                    }
                }



    Smile a lot, it costs nothing
    • Proposed as answer by ansafmmm Saturday, August 09, 2008 12:59 PM
    Saturday, August 09, 2008 12:46 PM
  • does the new node will be added at the end of the file?
    for example there is a Filexml.xml file, it has been added many nodes already, but later I will like to add more nodes, will the new nodes be saved at the end of the file, and keep the old nodes?
    Monday, June 08, 2009 8:30 PM