XmlTextWriter Csharp

  private void XmlWriteTest()
  {       
   string fileName="C://Test.xml";
   XmlTextWriter writer = new XmlTextWriter (fileName, null);
   //Use indenting for readability.
   writer.Formatting = Formatting.Indented;
       
   writer.WriteComment("sample XML fragment");
   
   //Write an element (this one is the root).
   writer.WriteStartElement("bookstore");

   //Write the namespace declaration.
   writer.WriteAttributeString("xmlns", "bk", null, "urn:samples");

   writer.WriteStartElement("book");

   //Lookup the prefix and then write the ISBN attribute.
   string prefix = writer.LookupPrefix("urn:samples");
   writer.WriteStartAttribute(prefix, "ISBN", "urn:samples");
   writer.WriteString("1-861003-78");
   writer.WriteEndAttribute();    

   //Write the title.
   writer.WriteStartElement("title");
   writer.WriteString("The Handmaid's Tale");
   writer.WriteEndElement();
             
   //Write the price.
   writer.WriteElementString("price", "19.95");
    
   //Write the style element.
   writer.WriteStartElement(prefix, "style", "urn:samples");
   writer.WriteString("hardcover");
   writer.WriteEndElement();

   //Write the end tag for the book element.
   writer.WriteEndElement();

   //Write the close tag for the root element.
   writer.WriteEndElement();
            
   //Write the XML to file and close the writer.
   writer.Flush();
   writer.Close();

   //Read the file back in and parse to ensure well formed XML.
   XmlDocument doc = new XmlDocument();
   //Preserve white space for readability.
   doc.PreserveWhitespace = true;
   //Load the file
   doc.Load(fileName);
   
   //Write the XML content to the console.
   MessageBox.Show(doc.InnerXml);

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数字化转型2025

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值