Append to XML using Xdocument

i have a XML file, i just need to append to it using Xdocument, can someone type the correct format of saving ? i know how to create using XDocument, but how do i append to an existing file?

Thanks
Raul77Asked:
Who is Participating?
 
ImperdonatoConnect With a Mentor Commented:
Or, use the following code:

Please note, the value for rootxPath will change depending on your xml.
For example, if you xml is:
<xml>
      <a>
      </a>
        <Countries>  
        </Countries>
</xml>

the value for rootxPath will be "xml/Countries".

Hope this helps!

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Countries.xml"));
strin rootxPath = "a/b/c/Countries";
XmlNode rootNode = doc.SelectSingleNode(rootxPath);
XmlNode Cname = doc.CreateNode(XmlNodeType.Element, "Country", doc.NamespaceURI);
XmlAttribute countryname = doc.CreateAttribute("Name");
countryname.InnerText = Country_Name_Text.Text;
Cname.Attributes.Append(countryname);
 
XmlNode Cpop = doc.CreateNode(XmlNodeType.Element, "Population", doc.NamespaceURI);
Cpop.InnerText = Country_POP_Text.Text;
Cname.AppendChild(Cpop);
 
XmlNode Cid = doc.CreateNode(XmlNodeType.Element, "ID", doc.NamespaceURI);
Cid.InnerText = Country_ID_Text.Text;
Cname.AppendChild(Cid);
 
rootNode.AppendChild(Cname);
doc.Save(Server.MapPath("Countries.xml"));

Open in new window

0
 
Anurag ThakurTechnical ManagerCommented:
0
 
Raul77Author Commented:
thanks, but i am not looking for merging XML file, I have a XML file, i want to be able to add more record at the end of the same file.

Thanks.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
ImperdonatoCommented:
User something like:

Please note however, that the value for 'xPath'  (and similarly other things) will change  according to your Xml.

Hope that helps.

string xmlStr = "<?xml version=\"1.0\" standalone=\"yes\"?><NewDataSet><Table1><EXPR_0>7</EXPR_0><EXPR_1>4</EXPR_1></Table1></
XmlDocument xDoc = new XmlDocument();
xDoc.LoadXml(xmlStr);
string xPath = "NewDataSet/Table1";
XmlNode nodeToAppend = xDoc.SelectSingleNode(xPath);
XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, "someName", xDoc.NamespaceURI);
newNode.InnerText = "EXPR_2";
nodeToAppend.AppendChild(newNode);

Open in new window

0
 
Raul77Author Commented:
this is how my XML look like:

<Countries>
  <Country Name="USA">
    <Population>300 Million</Population>
    <GDP>13,807,550 Millions of US$</GDP>
    <ID>1</ID>
  </Country>
</Countries>

now i would need to keep the exact same format and add Canada, i am very close, however the country tag is self closing. i need  it to close at the end.
this is my code so far:

        XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Countries.xml"));
        XmlElement Cname = doc.CreateElement("Country");
        XmlAttribute countryname = doc.CreateAttribute("Name");
        Cname.SetAttribute("Name", Country_Name_Text.Text);
        doc.DocumentElement.AppendChild(Cname);
        XmlElement Cpop = doc.CreateElement("Population");
        Cpop.InnerText = Country_POP_Text.Text;
        doc.DocumentElement.AppendChild(Cpop);
        XmlElement Cid = doc.CreateElement("ID");
        Cid.InnerText = Country_ID_Text.Text;
        doc.DocumentElement.AppendChild(Cid);        
        doc.Save(Server.MapPath("Countries.xml"));

the above code produces :

  <Country Name="Canada" />
  <Population>32 Mil</Population>
  <ID>5</ID>

but i want it to be:

  <Country Name="Canada">
  <Population>32 Mil</Population>
  <ID>5</ID>
 </Country>

Thanks,
0
 
ImperdonatoCommented:
Use this:

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Countries.xml"));
XmlNode Cname = doc.CreateNode(XmlNodeType.Element, "Country", doc.NamespaceURI);
XmlAttribute countryname = doc.CreateAttribute("Name");
countryname.InnerText = Country_Name_Text.Text;
Cname.Attributes.Append(countryname);
 
XmlNode Cpop = doc.CreateNode(XmlNodeType.Element, "Population", doc.NamespaceURI);
Cpop.InnerText = Country_POP_Text.Text;
Cname.AppendChild(Cpop);
 
XmlNode Cid = doc.CreateNode(XmlNodeType.Element, "ID", doc.NamespaceURI);
Cid.InnerText = Country_ID_Text.Text;
Cname.AppendChild(Cid);
 
doc.FirstChild.AppendChild(Cname);
doc.Save(Server.MapPath("Countries.xml"));

Open in new window

0
 
Raul77Author Commented:
doesnt compile :
The current node cannot contain other nodes.
doc.FirstChild.AppendChild(Cname);
0
 
ImperdonatoCommented:
Well I used the xml you provided in your earlier post:
<Countries>
  <Country Name="USA">
    <Population>300 Million</Population>
    <GDP>13,807,550 Millions of US$</GDP>
    <ID>1</ID>
  </Country>
</Countries>

and it worked.

Can you provide the exact xml you're using?
0
 
Raul77Author Commented:
that was the exact XML, it does actually compile, but when i try to add the country, i put the country name and ID and GDP and hit ADD it gives the above error.
0
 
ImperdonatoCommented:
Did you use the exact same code that I pasted above? That for sure works for me :-S
Please try once with the exact same code, with no modifications at all. I think there is something which we are missing, either your xml is different, or the code that you're using.

What's the initial xml, I mean, before you start appending data to it? The same thing which you posted, right? Should work then.

If it still doesn't work, please post the exact code that you're calling and the xml from countries.xml file.
0
 
Raul77Author Commented:
yup i used the EXACT code
here is the EXACT XML.
<?xml version="1.0"?>
<!--Created 2/17/09-->
<Countries>
   <Country Name="USA">
      <Population>300 Million</Population>
      <GDP>13,807,550 Millions of US$</GDP>
      <ID>1</ID>
   </Country>
   <Country Name="Canada">
      <Population>32 Million</Population>
      <GDP>1,436,086 Millions of US$</GDP>
      <ID>2</ID>
   </Country>
   <Country Name="Mexico">
      <Population>32 Million</Population>
      <GDP>1,022,816 Millions of US$</GDP>
      <ID>3</ID>
   </Country>
</Countries>

and here is the EXACT code:


 XmlDocument doc = new XmlDocument();
        doc.Load(Server.MapPath("Countries.xml"));
        XmlNode Cname = doc.CreateNode(XmlNodeType.Element, "Country", doc.NamespaceURI);
        XmlAttribute countryname = doc.CreateAttribute("Name");
        countryname.InnerText = Country_Name_Text.Text;
        Cname.Attributes.Append(countryname);
 
        XmlNode Cpop = doc.CreateNode(XmlNodeType.Element, "Population", doc.NamespaceURI);
        Cpop.InnerText = Country_POP_Text.Text;
        Cname.AppendChild(Cpop);
 
        XmlNode Cid = doc.CreateNode(XmlNodeType.Element, "ID", doc.NamespaceURI);
        Cid.InnerText = Country_ID_Text.Text;
        Cname.AppendChild(Cid);
 
        doc.FirstChild.AppendChild(Cname);
        doc.Save(Server.MapPath("Countries.xml"));

Open in new window

0
 
Raul77Author Commented:
Perfect :) thanks
0
 
ImperdonatoCommented:
Ok, so that was the problem... See the node "<?xml version="1.0"?>" at the top?

Please use the code in my last post, with the value for rootxPath set to "Countries"

That should work :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.