附加到现有的xml文件 [英] appending to an existing xml file

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

问题描述

我想写一个XML文件,但是,在某些情况下,我可能需要添加到一个包含封闭根元素的现有文件中.任何想法如何进行修改文件.

C#

I would like to write to an XML file, however, it might be the case that I might need to add to an existing file, which includes a closing root element. Any idea how to go about amending the file.

C#

string _path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
            _path = _path.Substring(6, _path.Length - 6);
            string _file =  _path + "\\article.xml";

            FileStream fs = new FileStream(_file, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, null);



article.xml



article.xml

<pre lang="xml"><?xml version="1.0" encoding="ISO-8859-1"?>
<articles>
    <article>
      <timestamp>24/08/2010 15:12</timestamp>
      <subject> subject</subject>
      <body>this is a test</body>
      <entryID>000001</entryID>
      <deleted>No</deleted>
      <tag>tag1,tag2</tag>
    </article>
    <article>
      <timestamp>24/08/2010 15:13</timestamp>
      <subject> subject 2</subject>
      <body>this is a test</body>
      <entryID>000001</entryID>
      <deleted>No</deleted>
      <tag>tag1,tag2</tag>
    </article>
</articles>




:confused:


注意:注意,




:confused:


NOTE: It''s noted that the sequence following the

tag consisting of "<pre lang="xml">&lt;?" is not rendered as text within this forum.

推荐答案

之后的顺序要更改Xml文件,请使用XMLDocument类和DOM插入节点.您不能在说在这里开始写"的地方插入XML,因为您的文档必须恰好有一个根节点.
To change an Xml file, you''d use the XMLDocument class and the DOM to insert nodes. You can''t insert XML where you say ''start writing here'' because your document must have exactly one root node.


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

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