使用XmlWriter附加到XML文件 [英] Append to XML file using XmlWriter

查看:71
本文介绍了使用XmlWriter附加到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 XmlDocument XmlWriter 将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

此文档已具有"DocumentElement"节点.

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>

推荐答案

如果使用的是.net 3.5版,则要使用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-到XML

使用XPath和XmlDocument(C#)

OR

文章:如何附加到大型XML文件

我想您需要像这样将节点附加到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");

结果将是这样:

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

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

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

这篇关于使用XmlWriter附加到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆