众所周知~XmlWriter类 与 DOM 的区别,前者是用流写入器写入内容,资源消耗很少。可问题是,我从书上看来,.net对XmlWriter有个支持类XmlTextWriter,该类在写入文件的时候,是重新写的,也就是说,如果我想在原有的xml文档后面接着写入内容,恐怕不行~~
是不是这样呢?如果是这样的话,有没有什么解决方案呢?

解决方案 »

  1.   

    XmlTextWriter 是覆盖方式吗?没实际写过用 xmldocument 或 xmldatadocument 吧
      

  2.   

    一直用xmldocument 的,xmlWriter没用过,不过stream似乎是有这样的局限
      

  3.   

    xml is normally in the text format, if it is a normal text file, you could append contents to the end of the file, but since xml has a start tag and end tag, so you need to INSERT your content before the end tag, that is not possible for text format (if you want to keep the integrity of the xml format)
      

  4.   

    用xmldocument 写的话~~~~如果文档太大就吃不消了哦……再等等看~~~~有没有什么好方法阿~~我想如果不能续写,那XmlTextWriter也太差劲了吧~~谁还会用XmlTextWriter阿~~
      

  5.   

    saucer(思归) 同志说的有道理,可是就没有别的补救方法了?或者有什么技巧上的东西?比如说在最后的结束标记前的位置插入...?嘿嘿
      

  6.   

    在看了你的问题后写了如下代码:FileStream tempstream = new FileStream("1.xml",System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite);
    XmlTextWriter temp = new XmlTextWriter(tempstream,System.Text.Encoding.UTF8);
    tempstream.Position = 5; temp.WriteStartElement("ss","s1","s11");
    temp.WriteString("sss");
    temp.WriteEndElement(); temp.Flush();
    byte[] tempbyte = new byte[6];
    string temps = "</xxx>";
    for(int i=0;i<6;i++)
    {
    tempbyte[i] = Convert.ToByte(temps[i]);
    }
    tempstream.Write(tempbyte,0,6);其中xml开始为
    <xxx></xxx>写后为<xxx>&#65279;<ss:s1 xmlns:ss="s11">sss</ss:s1></xxx>
      

  7.   

    做法还是以文件流入手。因为反编译后发现XmlTextWriter构造函数的Stream传到了其私有变量this.textWriter里,并没有重新new,所以可以从操作原来的流着手
      

  8.   

    多谢楼上的,新鲜的答案阿~~保存先~~一定给分不过,这最终还是FileStream来写的哦~~我非常想知道有没有其它派生于XmlWriter的官方或非官方的类,通过内置的巧妙的方法可以直接续写的,而不用再借助于FileStream~~~
      

  9.   

    XmlTextWriter是有个构造函数可以写入给定的流的,难道微软就是让我们用FileStream的流,难道这就是微软给我的最终解决方案???晕哦