使用xmlwriter附加xml文件
感谢以前的建议,使用“XMLWriter”,每次它创建新的XML文件,所以我用xmldoc加载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操作XML数据(C#)
要么
文章:如何附加到一个大的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文档中
链接地址: http://www.djcxy.com/p/57361.html