//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
要么
Manipulate XML data with XPath and XmlDocument (C#)
要么
文章:How to Append to a Large XML File
我想你需要像这样将节点附加到你的xmldocuemnt
//add to elements collectiondoc.documentElement.AppendChild(node);
你需要做这样的事情
Xmldocument xmlDoc=new Xmldocument();xmlDoc.Load("F:/documents and Settings/administrator/Desktop/Account.xml");XmlElement subroot=xmlDoc.CreateElement("User");//UsernameXmlElement appendedElementUsername=xmlDoc.CreateElement("Username");XmlText xmlTextUsername=xmlDoc.CreateTextNode(txtUsrname.Text.Trim());appendedElementUsername.AppendChild(xmlTextUsername);subroot.AppendChild(appendedElementUsername);xmlDoc.documentElement.AppendChild(subroot);//EmailXmlElement 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");//UsernameXmlElement appendedElementUsername=xmlDoc.CreateElement("Username");XmlText xmlTextUsername=xmlDoc.CreateTextNode(txtUsrname.Text.Trim());appendedElementUsername.AppendChild(xmlTextUsername);subroot.AppendChild(appendedElementUsername);xmlDoc.documentElement.AppendChild(subroot);//EmailXmlElement 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:Append in xml document
总结以上是内存溢出为你收集整理的c# – 使用xmlwriter附加xml文件全部内容,希望文章能够帮你解决c# – 使用xmlwriter附加xml文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)