使用的XmlWriter追加xml文件 [英] append xml file using xmlwriter
问题描述
感谢您的早期建议用XMLWriter的,每次它会创建新的XML文件,所以我用xmlDoc中加载XML文件,然后在该文件中追加,这里是我的代码,但它会抛出异常说这个文件已经有了一个DocumentElement节点。
Thanks for the earlier suggestion to use "XMLWriter" ,everytime it creates new xml file so i used xmldoc to load the xml file then append in that file,here is my code but it throws exception saying "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();
}
下面是我的样本的test.xml
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>
我试图在XML中追加像下面
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>
在此先感谢
推荐答案
要与XML数据玩,如果你使用的是.NET版本3.5其更好地用户的 的LINQ to XML 。
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
或
XML数据<强>或
文章:的如何追加到大型XML文件
我thnik需要追加节点您xmldocuemnt这样
I thnik you need to append node to your xmldocuemnt like this
//add to elements collection
doc.DocumentElement.AppendChild(node);
您需要做这样的事情。
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");
的
result'll是这样的:
The result'll be like that:
</USERS>
<User>
<UserName>Buggaya</UserName>
<Email>Buggaya@gmail.com</Email>
</User>
</USERS>
一部开拓创新的帖子:的追加XML文档中
这篇关于使用的XmlWriter追加xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!