使用XmlWriter将内容追加到XML文件中

7
我正在使用 XmlDocumentXmlWriter 将 XML 追加到现有文件中,但是我的尝试会抛出一个我不理解的异常:

此文档已经具有“DocumentElement”节点。

//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 文件。
<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中追加内容:

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

这有帮助吗? - Pranay Rana
非常感谢,例如我还没有修改,现在我会开始编写一个代码,并将其发布在这里。再次感谢。 - Usher
1个回答

4
若使用 .net 3.5 版本,想处理 XML 数据最好使用 LINQ to XML。以下是相关链接:LINQ to XMLLINQ to XML 实例
或者,您也可以使用 XPath 和 XmlDocument (C#) 的方式来操作 XML 数据,相关链接:XPath 和 XmlDocument (C#) 实例
另外,如需将节点附加到 XML 文档,请参考文章 How to Append to a Large XML File
//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");

结果会像这样:
</USERS>
<User>
<UserName>Buggaya</UserName> 

<Email>Buggaya@gmail.com</Email> 
</User>
</USERS>

原始帖子:在xml文档中添加内容


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接