.NET操作XML--XMLTextWriter

本文介绍如何使用C#中的XMLTextWriter类创建符合W3C规范的XML文件,并演示了如何利用Base64编码处理字符串数组。XMLTextWriter采用只前方式输出XML数据,能自动补全闭合标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[c-sharp]  view plain copy print ?
  1. void CreateXMLFileUsingWriters(String[] theArray, string filename)   
  2.   
  3. {   
  4.   
  5. // Open the XML writer (用默认的字符集)   
  6.   
  7. XMLTextWriter xmlw = new XmlTextWriter(filename, null);   
  8.   
  9. XMLw.Formatting = Formatting.Indented;   
  10.   
  11. XMLw.WriteStartDocument();   
  12.   
  13. XMLw.WriteStartElement("array");   
  14.   
  15.  XML Writer类以只前(forward-only)的方式输出XML数据到流或者文件中。更重要的是,XML Writer在设计时就保证所有的XML数据都符合W3C XML 1.0推荐规范,你甚至不用担心忘记写闭标签,因为XML Writer会帮你写。XmlWriter是所有 XML writer的抽象基类。.NET Framework只提供唯一的一个writer 类----XmlTextWriter类foreach(string s in theArray)   
  16.   
  17. {   
  18.   
  19. XMLw.WriteStartElement("element");   
  20.   
  21. XMLw.WriteAttributeString("value", s);   
  22.   
  23. XMLw.WriteEndElement();   
  24.   
  25. }   
  26.   
  27. XMLw.WriteEndDocument();   
  28.   
  29. // Close the writer   
  30.   
  31. XMLw.Close();   
  32.   
  33. }   

 

然而XML writer并不是魔术师----它不能修复输入的错误。XML writer不会检查元素名和属性名是否有效,也不保证被用的任何的Unicode字符集适合当前架构的编码集。如上所述,为了避免输出错误,必须要杜绝非XML字符。但是writer没有提供这种方法。

XML writer 还有一些状态值(state)。这些值都源于WriteState枚举类。当你创建一个Writer,它的初始状态为Start,表示你将要配置该对象,实际上writer没有开始。下一个状态是Prolog,该状态是当你调用WriteStartDocument方法开始工作的时候设置的。然后,状态的转换就取决于你的写的文档及文档的内容了。Prolog状态一直保留到当你增加一个非元素节点时,例如注释元素,处理指令及文档类型。当第一个节点也就是根节点写完后,状态就变为Element。当你调用WriterStartAtribute方法时状态转换为Attribute,而不是当你调用WriteAtributeString方法写属性时转换为该状态。如果那样的话,状态应该是Element。当你写一个闭标签(>)时,状态会转换成Content。当你写完文档后,调用WriteEndDocument方法,状态就会返回为Start,直到你开始写另一个文档或者把Writer关掉。

State

Description

Attribute

The writer enters this state when an attribute is being written

Closed

The Close method has been called and the writer is no longer available for writing operations

Content

The writer enters this state when the content of a node is being written

Element

The writer enters this state when an element start tag is being written

Prolog

The writer is writing the prolog of a well-formed XML 1.0 document

Start

The writer is in an initial state, awaiting for a write call to be issued

  Writer 把输出文本存在内部的一个缓冲区内。一般情况下,缓冲区会被刷新或者被清除,当Writer被关闭前XML文本应该要写出。在任何时你都可以通过调用Flush方法清空缓冲区,把当前的内容写到流中(通过BaseStream属性暴露流),然后释放部分占用的内存,Writer仍保持为打开状态(open state),可以继续操作。注意,虽然写了部分的文档内容,但是在Writer没有关闭前其它的程序是不能处理该文档的。

  可以用两种方法来写属性节点。第一种方法是用WriteStartAtribute方法去创建一个新的属性节点,更新Writer的状态。接着用WriteString方法设置属性值。写完后,用WriteEndElement方法结束该节点。另外,你也可以用WriteAttributeString方法去创建新的属性节点,当writerr的状态为Element时,WriterAttributeString开始工作,它单独创建一个属性。同样的,WriteStartElement方法写节点的开始标签(<),然后你可以随意的设置节点的属性和文本内容。元素节点的闭标签都带”/ >”。如果想写闭标签可以用WriteFullEndElement方法来写。

  应该避免传送给写方法的文本中包含敏感的标记字符,例如小于号(<)。用WriteRaw方法写入流的字符串不会被解析,我们可以用它来对XML文档写入特殊的字符串。下面的两行代码,第一行输出的是”<”,第二行输出”<”:

writer.WriteString("<");

writer.WriteRaw("<");

读写流

  有趣的是,reader(阅读器)和writer类提供了基于Base64 和BinHex编码的读写数据流的方法。WriteBase64 和 WriteBinHex方法的功能与其它的写方法的功能存在着细微的差别。它们都是基于流的,这两个方法的功能像一个byte数组而不是一个string。下面的代码首先把一个string转换成一个byte数组,然后把它们写成一个Base64 编码流。Encoding类的GetBytes静态方法完成转换的任务:

writer.WriteBase64(

Encoding.Unicode.GetBytes(buf),

0, buf.Length*2);

以下是写入和读取XML文档的示例:

Figure 8 Persisting a String Array as Base64

 

[c-sharp]  view plain copy print ?
  1. using System;   
  2.   
  3. using System.Text;   
  4.   
  5. using System.IO;   
  6.   
  7. using System.XML;   
  8.   
  9. class MyBase64Array   
  10.   
  11. {   
  12.   
  13. public static void Main(String[] args)   
  14.   
  15. {   
  16.   
  17. string outputFileName = "test64.XML";   
  18.   
  19. if (args.Length > 0)   
  20.   
  21. outputFileName = args[0]; // file name   
  22.   
  23. // 把数组转换成XML   
  24.   
  25. String[] theArray = {"Rome""New York""Sydney""Stockholm",   
  26.   
  27. "Paris"};   
  28.   
  29. CreateOutput(theArray, outputFileName);   
  30.   
  31. return;   
  32.   
  33. }   
  34.   
  35. private static void CreateOutput(string[] theArray, string filename)   
  36.   
  37. {   
  38.   
  39. // 打开XML writer   
  40.   
  41. XMLTextWriter xmlw = new XmlTextWriter(filename, null);   
  42.   
  43. //使子元素根据 Indentation 和 IndentChar 设置缩进。此选项只对元素内容进行缩进   
  44.   
  45. XMLw.Formatting = Formatting.Indented;   
  46.   
  47. //书写版本为“1.0”的 XML 声明   
  48.   
  49. XMLw.WriteStartDocument();   
  50.   
  51. //写出包含指定文本的注释 。   
  52.   
  53. XMLw.WriteComment("Array to Base64 XML");   
  54.   
  55. //开始写出array节点   
  56.   
  57. XMLw.WriteStartElement("array");   
  58.   
  59. //写出具有指定的前缀、本地名称、命名空间 URI 和值的属性   
  60.   
  61. XMLw.WriteAttributeString("xmlns""x"null"dinoe:msdn-mag");   
  62.   
  63. // 循环的写入array的子节点   
  64.   
  65. foreach(string s in theArray)   
  66.   
  67. {   
  68.   
  69. //写出指定的开始标记并将其与给定的命名空间和前缀关联起来   
  70.   
  71. XMLw.WriteStartElement("x""element"null);   
  72.   
  73. //把S转换成byte[]数组, 并把byte[]数组编码为 Base64 并写出结果文本,要写入的字节数为s总长度的2倍,一个string占的字节数是2字节。   
  74.   
  75. XMLw.WriteBase64(Encoding.Unicode.GetBytes(s), 0, s.Length*2);   
  76.   
  77. //关闭子节点   
  78.   
  79. XMLw.WriteEndElement();   
  80.   
  81. }   
  82.   
  83. //关闭根节点,只有两级   
  84.   
  85. XMLw.WriteEndDocument();   
  86.   
  87. // 关闭writer   
  88.   
  89. XMLw.Close();   
  90.   
  91. // 读出写入的内容   
  92.   
  93. XMLTextReader reader = new XmlTextReader(filname);   
  94.   
  95. while(reader.Read())   
  96.   
  97. {   
  98.   
  99. //获取节点名为element的节点   
  100.   
  101. if (reader.LocalName == "element")   
  102.   
  103. {   
  104.   
  105. byte[] bytes = new byte[1000];   
  106.   
  107. int n = reader.ReadBase64(bytes, 0, 1000);   
  108.   
  109. string buf = Encoding.Unicode.GetString(bytes);   
  110.   
  111. Console.WriteLine(buf.Substring(0,n));   
  112.   
  113. }   
  114.   
  115. }   
  116.   
  117. reader.Close();   
  118.   
  119. }   
  120.   
  121. }   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值