使用xmlwriter附加xml文件

感谢早先建议使用“XMLWriter”,每次创建新的xml文件,所以我使用xmldoc加载xml文件然后追加到该文件中,这是我的代码,但它抛出exception说“这个文档已经有’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

   730s_Sales/CupWinner_0111 Africa Africa Leigh Sales Current CBS true  

我试图像下面的xml一样追加

   730s_Sales/CupWinner_0111 Africa Africa Leigh Sales Current CBS true -  80 60 7045 JPEG Baseline 72 newsthumbnail   

提前致谢

要使用.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"); 

结果就是这样:

   Buggaya Buggaya@gmail.com   

orignal post: 附加在xml文档中