先保存xml文件,用文本方式打开,添加

解决方案 »

  1.   

    frankly, Oceanson's method is not so badusing System;
    using System.Xml;class TestXml
    {
      public static void Main()
      {
    String sFile = "a.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(sFile);

    XmlProcessingInstruction newPI;
    String PItext = "type='text/xsl' href='book.xsl'";
    newPI = doc.CreateProcessingInstruction("xml-stylesheet", PItext);
    doc.InsertBefore(newPI, doc.FirstChild); XmlTextWriter xtw = new XmlTextWriter(sFile, System.Text.Encoding.GetEncoding("GB2312"));
    xtw.Formatting = Formatting.Indented;
    xtw.WriteStartDocument();
    doc.Save(xtw);
    xtw.WriteEndDocument();
    xtw.Close();
       }
    }