XmlTextWriter类可以把XML写入一个流、文件或TextWriter对象中

来源:互联网 发布:淘宝天猫京东对比 编辑:程序博客网 时间:2024/06/05 05:41

  private void button2_Click(object sender, System.EventArgs e)
  {
   string filename = "booknew.xml";
   XmlTextWriter tw = new XmlTextWriter(filename,null);
   tw.Formatting = Formatting.Indented;
   tw.WriteStartDocument();
   
   tw.WriteStartElement("book");
   tw.WriteAttributeString("genre","Mystery");
   tw.WriteAttributeString("publicationdate","2001");
   tw.WriteAttributeString("ISBN","123456789");
   tw.WriteElementString("title","Case of the Missing Cookie");
   tw.WriteStartElement("author");
   tw.WriteElementString("name","Cookie Monster");
   tw.WriteEndElement();
   tw.WriteElementString("price","9.99");

string ss="hello world";
 tw.WriteStartElement("hello");
 tw.WriteCData(ss);
 tw.WriteEndElement();

   tw.WriteStar
   tw.WriteEndElement();
   tw.WriteEndDocument();
   tw.Flush();
   tw.Close();
  }