We help IT Professionals succeed at work.

Append to XML using Xdocument

Raul77
Raul77 asked
on
Medium Priority
1,432 Views
Last Modified: 2013-11-10
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
Comment
Watch Question

Author

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.
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

Author

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,
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

Author

Commented:
doesnt compile :
The current node cannot contain other nodes.
doc.FirstChild.AppendChild(Cname);
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?

Author

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.
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.

Author

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

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Perfect :) thanks
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 :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.