Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append to XML file using XmlWriter

Tags:

c#

xml

I'm using XmlDocument and XmlWriter to append XML into an existing file, but my attempt below is throwing an exception that I don't understand

This document already has a 'DocumentElement' node.

//Append to xml file

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\\test.xml");
using (XmlWriter xmlWrite = doc.CreateNavigator().AppendChild())
{
    xmlWrite.WriteStartElement("image name=",Name);
    xmlWrite.WriteElementString("width", widthValue[1]);
    xmlWrite.WriteElementString("Height", heightValue[1]);
    xmlWrite.WriteElementString("file-size", FileSizeValue[1]);
    xmlWrite.WriteElementString("file-format", FileFormatValue[1]);
    xmlWrite.WriteElementString("resolution", ResolutionValue[1]);
    xmlWrite.Close();
}

here is my sample test.xml

<job-metadata>
    <slug>730s_Sales/CupWinner_0111</slug>
    <locations>Africa</locations>
    <primary-location>Africa</primary-location>
    <reporter>Leigh Sales</reporter>
    <genre>Current</genre>
    <copyright>CBS</copyright>
    <autopublish>true</autopublish> 
</job-metadata>

Am trying to append in the xml like below

<job-metadata>
    <slug>730s_Sales/CupWinner_0111</slug>
    <locations>Africa</locations>
    <primary-location>Africa</primary-location>
    <reporter>Leigh Sales</reporter>
    <genre>Current</genre>
    <copyright>CBS</copyright>
    <autopublish>true</autopublish> 
    <image name="557684_20111101-730s_SalesCupWinner_0111_80x60.jpg">
        <width>80</width>
        <height>60</height>
        <file-size>7045</file-size>
        <file-format>JPEG Baseline</file-format>
        <resolution>72</resolution>
        <custom-name>newsthumbnail</custom-name>
    </image>
</job-metadata>
like image 956
Usher Avatar asked Feb 08 '12 06:02

Usher


1 Answers

To play with the XML data if you are using .net version 3.5 its better to user LINQ to XML.

http://www.codeproject.com/Articles/24376/LINQ-to-XML

or

Manipulate XML data with XPath and XmlDocument (C#)

OR

Article : How to Append to a Large XML File

I thnik you need to append node to your xmldocuemnt like this

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

You need to do something like this

XmlDocument xmlDoc=new XmlDocument();

xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml");

XmlElement subRoot=xmlDoc.CreateElement("User");
//UserName
XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName");
XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim());
appendedElementUsername.AppendChild(xmlTextUserName);
subRoot.AppendChild(appendedElementUsername);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Email

XmlElement appendedElementEmail=xmlDoc.CreateElement("Email");
XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim());
appendedElementEmail.AppendChild(xmlTextEmail);
subRoot.AppendChild(appendedElementEmail);
xmlDoc.DocumentElement.AppendChild(subRoot);

xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");if(!File.Exists("F:/Documents and Settings/Administrator/Desktop/Account.xml"))
{

XmlTextWriter textWritter=new XmlTextWriter("F:/Documents and Settings/Administrator/Desktop/Account.xml", null); 
textWritter.WriteStartDocument();
textWritter.WriteStartElement("USERS");
textWritter.WriteEndElement();

textWritter.Close();
}



XmlDocument xmlDoc=new XmlDocument();

xmlDoc.Load("F:/Documents and Settings/Administrator/Desktop/Account.xml");

XmlElement subRoot=xmlDoc.CreateElement("User");
//UserName
XmlElement appendedElementUsername=xmlDoc.CreateElement("UserName");
XmlText xmlTextUserName=xmlDoc.CreateTextNode(txtUsrName.Text.Trim());
appendedElementUsername.AppendChild(xmlTextUserName);
subRoot.AppendChild(appendedElementUsername);
xmlDoc.DocumentElement.AppendChild(subRoot);
//Email

XmlElement appendedElementEmail=xmlDoc.CreateElement("Email");
XmlText xmlTextEmail=xmlDoc.CreateTextNode(txtEmail.Text.Trim());
appendedElementEmail.AppendChild(xmlTextEmail);
subRoot.AppendChild(appendedElementEmail);
xmlDoc.DocumentElement.AppendChild(subRoot);

xmlDoc.Save("F:/Documents and Settings/Administrator/Desktop/Account.xml");

The result'll be like that:

</USERS>
<User>
<UserName>Buggaya</UserName> 

<Email>[email protected]</Email> 
</User>
</USERS>

orignal post : Append in xml document

like image 79
Pranay Rana Avatar answered Sep 28 '22 19:09

Pranay Rana