如何附加到xml文件 [英] How do to append to an xml fIle

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

问题描述

嗨大家好



好​​吧问题是这样我有以下代码



Hi Guys

Well the problem is like this i have the following code

XmlTextWriter writer = new XmlTextWriter("D:\\Grop.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.WriteStartElement("Table");
             //calling the createNode method 
            createNode("123", "3232", "43234","A string of text 1",DateTime.Now.ToString(), writer);
            createNode("123", "3232", "43234", "A string of text 2", DateTime.Now.ToString(), writer);
            createNode("123", "3232", "43234", "A string of text 3", DateTime.Now.ToString(), writer);
            createNode("123", "3232", "43234", "A string of text 4", DateTime.Now.ToString(), writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();





//创建节点方法



//the create node method

private void createNode(string stud_Id, string post_Id, string group_Id,string desc, string postdate, XmlTextWriter writer)
       {
           writer.WriteStartElement("Post");

           writer.WriteStartElement("Student_ID");
           writer.WriteString(stud_Id);
           writer.WriteEndElement();


           writer.WriteStartElement("Group_ID");
           writer.WriteString(group_Id);
           writer.WriteEndElement();


           writer.WriteEndElement();
       }







问题是上面的代码没有附加到xml文件,每次我编译代码时它只会覆盖文件的内容。每次编译和运行代码时如何附加?



提前致谢




The problem is this the code above doesnt append to the xml file,it only overwrites the contents of the file everytime i complie the code. How do I get to append each time I compile and run the code?

Thanks in advance

推荐答案

以下文章讨论XLinq以及如何插入/删除。最好的选择是将xml加载到内存中,并使用XLinq等技术进行更新。这可能有所帮助: XLINQ简介第3部分,共3部分 [< a href =http://www.codeproject.com/Articles/18751/XLINQ-Introduction-Part-3-Of-3target =_ blanktitle =New Window> ^ ] br />


您可以插入,但这需要大量的工作,因为您必须在非常低的水平上进行。您必须找到必须插入更改的位置,然后创建空间。你会做很多工作。如果你需要插入东西,并且不想在内存中进行,你可能想要使用数据库。
The following article discusses XLinq and how to insert/delete. The best option is to load the xml into memory, and update using a technology such as XLinq. This might be of help: XLINQ Introduction Part 3 Of 3[^]

You might be able to insert, but that would require a lot of work since you would have to do it at a very low level. You have to find where the change has to be inserted, then make the space. You would be doing a lot of work. If you need to insert stuff, and don''t want to do it in memory, you might want to use a database.


我想你必须阅读XML文件在第一个(例如XmlTextReader?)中,将节点插入你想要的位置(即在内存中),然后将整个内容写回文件。



问候,

Ian。
I think you''ll have to read the XML file in first (e.g XmlTextReader ?), insert the nodes where you want them (i.e. in memory) and then write the whole thing back to the file.

Regards,
Ian.


试试这篇文章它会帮助你...检查1答案

http://stackoverflow.com/questions/9188574/append-xml-file-using-xmlwriter [ ^ ]
try this article it will help u..check 1 Answer
http://stackoverflow.com/questions/9188574/append-xml-file-using-xmlwriter[^]


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

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