簡體   English   中英

使用XmlWriter附加到XML文件

[英]Append to XML file using XmlWriter

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

要使用.net版本3.5,要使用XML數據,最好使用LINQ to XML

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

要么

使用XPath和XmlDocument(C#)處理XML數據

要么

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

我想你需要像這樣將節點附加到你的xmldocuemnt

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

orignal post: 附加在xml文檔中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM