{"id":518,"date":"2009-07-06T16:54:00","date_gmt":"2009-07-06T08:54:00","guid":{"rendered":""},"modified":"2013-11-17T13:16:37","modified_gmt":"2013-11-17T05:16:37","slug":"c%e5%a6%82%e4%bd%95%e8%af%bb%e5%86%99xml%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/518.html","title":{"rendered":"c#\u5982\u4f55\u8bfb\u5199xml\u6587\u4ef6"},"content":{"rendered":"<p>\u4e00\uff0e\u524d\u8a00\uff1a<br \/>\nXML\u662f\u5fae\u8f6f.Net\u6218\u7565\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u800c\u4e14\u5b83\u53ef\u8c13\u662fXML Web\u670d\u52a1\u7684\u57fa\u77f3\uff0c\u6240\u4ee5\u638c\u63e1.Net\u6846\u67b6\u4e0b\u7684XML\u6280\u672f\u81ea\u7136\u663e\u5f97\u975e\u5e38\u91cd\u8981\u4e86\u3002\u672c\u6587\u5c06\u6307\u5bfc\u5927\u5bb6\u5982\u4f55\u8fd0\u7528C#\u8bed\u8a00\u5b8c\u6210.Net\u6846\u67b6\u4e0b\u7684XML\u6587\u6863\u7684\u8bfb\u5199\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u4f1a\u5411\u5927\u5bb6\u4ecb\u7ecd.Net\u6846\u67b6\u4e2d\u4e0eXML\u76f8\u5173\u7684\u547d\u540d\u7a7a\u95f4\u548c\u5176\u4e2d\u7684\u91cd\u8981\u7c7b\u3002\u5176\u6b21\uff0c\u6211\u8fd8\u4f1a\u7ed9\u51fa\u6709\u5173\u7684\u5b9e\u4f8b\u4ee5\u4f7f\u8bfb\u8005\u66f4\u8fdb\u4e00\u6b65\u7684\u4e86\u89e3XML\u6587\u6863\u7684\u8bfb\u5199\u64cd\u4f5c\u7684\u5177\u4f53\u65b9\u6cd5\u3002 <\/p>\n<p>\u4e8c\uff0eXML\u547d\u540d\u7a7a\u95f4\u548c\u76f8\u5173\u7c7b\u7b80\u4ecb\uff1a<br \/>\n\u5728\u6df1\u5165\u8fdb\u884c.Net\u6846\u67b6\u4e0b\u7684XML\u6587\u6863\u7684\u64cd\u4f5c\u4e4b\u524d\uff0c\u6211\u60f3\u5f88\u6709\u5fc5\u8981\u5411\u5927\u5bb6\u4ecb\u7ecd.Net\u6846\u67b6\u4e2d\u4e0eXML\u6280\u672f\u6709\u5173\u7684\u547d\u540d\u7a7a\u95f4\u548c\u5176\u4e2d\u4e00\u4e9b\u91cd\u8981\u7684\u7c7b\u3002.Net\u6846\u67b6\u4e3a\u6211\u4eec\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u4e00\u4e9b\u547d\u540d\u7a7a\u95f4\uff1aSystem.Xml\u3001System.Xml.Schema\u3001 System.Xml.Serialization\u3001System.Xml.Xpath\u4ee5\u53ca System.Xml.Xsl\u6765\u5305\u5bb9\u548cXML\u64cd\u4f5c\u76f8\u5173\u7684\u7c7b\u3002<br \/>\nSystem.Xml\u547d\u540d\u7a7a\u95f4\u5305\u542b\u4e86\u4e00\u4e9b\u6700\u91cd\u8981\u7684XML\u7c7b\uff0c\u5176\u4e2d\u6700\u4e3b\u8981\u7684\u7c7b\u662f\u548cXML\u6587\u6863\u7684\u8bfb\u5199\u64cd\u4f5c\u76f8\u5173\u7684\u7c7b\u3002\u8fd9\u4e9b\u7c7b\u4e2d\u5305\u62ec4\u4e2a\u4e0e\u8bfb\u76f8\u5173\u7684\u7c7b\u4ee5\u53ca2\u4e2a\u4e0e\u5199\u76f8\u5173\u7684\u7c7b\u3002\u5b83\u4eec\u5206\u522b\u662f\uff1aXmlReader\u3001XmlTextReader\u3001 XmlValidatingReader\u3001XmlNodeReader\u3001XmlWriter\u4ee5\u53ca XmlTextWriter\u3002\u672c\u6587\u5c06\u91cd\u70b9\u4ecb\u7ecd\u8fd9\u4e9b\u7c7b\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6700\u57fa\u672c\u4e5f\u662f\u6700\u91cd\u8981\u7684\u7c7b\u3002<br \/>\nXmlReader\u7c7b\u662f\u4e00\u4e2a\u865a\u57fa\u7c7b\uff0c\u5b83\u5305\u542b\u4e86\u8bfbXML\u6587\u6863\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u8be5\u7c7b\u4e2d\u7684Read\u65b9\u6cd5\u662f\u4e00\u4e2a\u57fa\u672c\u7684\u8bfbXML\u6587\u6863\u7684\u65b9\u6cd5\uff0c\u5b83\u4ee5\u6d41\u5f62\u5f0f\u8bfb\u53d6XML\u6587\u6863\u4e2d\u7684\u8282\u70b9\uff08Node\uff09\u3002\u53e6\u5916\uff0c\u8be5\u7c7b\u8fd8\u63d0\u4f9b\u4e86ReadString\u3001ReadInnerXml\u3001 ReadOuterXml\u548cReadStartElement\u7b49\u66f4\u9ad8\u7ea7\u7684\u8bfb\u65b9\u6cd5\u3002\u9664\u4e86\u63d0\u4f9b\u8bfbXML\u6587\u6863\u7684\u65b9\u6cd5\u5916\uff0cXmlReader\u7c7b\u8fd8\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86 MoveToAttribute\u3001MoveToFirstAttribute\u3001MoveToContent\u3001MoveToFirstContent\u3001 MoveToElement\u4ee5\u53ca MoveToNextAttribute\u7b49\u5177\u6709\u5bfc\u822a\u529f\u80fd\u7684\u65b9\u6cd5\u3002\u5728\u672c\u6587\u540e\u9762\u4ecb\u7ecd\u7684\u5b9e\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u8fd0\u7528\u5230\u8fd9\u4e9b\u65b9\u6cd5\u3002<br \/>\nXmlTextReader\u3001XmlNodeReader\u4ee5\u53caXmlValidatingReader\u7b49\u7c7b\u662f\u4eceXmlReader\u7c7b\u7ee7\u627f\u8fc7\u6765\u7684\u5b50\u7c7b\u3002\u6839\u636e\u5b83\u4eec\u7684\u540d\u79f0\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u5176\u4f5c\u7528\u5206\u522b\u662f\u8bfb\u53d6\u6587\u672c\u5185\u5bb9\u3001\u8bfb\u53d6\u8282\u70b9\u548c\u8bfb\u53d6XML\u6a21\u5f0f\uff08Schemas\uff09\u3002<br \/>\nXmlWriter\u7c7b\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u8bb8\u591a\u5199XML\u6587\u6863\u7684\u65b9\u6cd5\uff0c\u5b83\u662fXmlTextWriter\u7c7b\u7684\u57fa\u7c7b\uff0c\u6211\u5728\u540e\u9762\u7684\u5b9e\u4f8b\u4e2d\u4f1a\u7ed9\u51fa\u76f8\u5173\u7684\u8fd0\u7528\u65b9\u6cd5\u3002<br \/>\nXmlNode\u7c7b\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684\u7c7b\uff0c\u5b83\u4ee3\u8868\u4e86XML\u6587\u6863\u4e2d\u7684\u67d0\u4e2a\u8282\u70b9\u3002\u8be5\u8282\u70b9\u53ef\u4ee5\u662fXML\u6587\u6863\u7684\u6839\u8282\u70b9\uff0c\u8fd9\u6837\u5b83\u5c31\u4ee3\u8868\u6574\u4e2aXML\u6587\u6863\u4e86\u3002\u5b83\u662f\u8bb8\u591a\u5f88\u6709\u7528\u7684\u7c7b\u7684\u57fa\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u5305\u62ec\u63d2\u5165\u8282\u70b9\u7684\u7c7b\u3001\u5220\u9664\u8282\u70b9\u7684\u7c7b\u3001\u66ff\u6362\u8282\u70b9\u7684\u7c7b\u4ee5\u53ca\u5728XML\u6587\u6863\u4e2d\u5b8c\u6210\u5bfc\u822a\u529f\u80fd\u7684\u7c7b\u3002\u540c\u65f6\uff0cXmlNode\u7c7b\u8fd8\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u83b7\u53d6\u53cc\u4eb2\u8282\u70b9\u3001\u5b50\u8282\u70b9\u3001\u6700\u540e\u4e00\u4e2a\u5b50\u8282\u70b9\u3001\u8282\u70b9\u540d\u79f0\u4ee5\u53ca\u8282\u70b9\u7c7b\u578b\u7b49\u7684\u5c5e\u6027\u3002\u5b83\u7684\u4e09\u4e2a\u6700\u4e3b\u8981\u7684\u5b50\u7c7b\u5305\u62ec\uff1a XmlDocument\u3001XmlDataDocument\u4ee5\u53caXmlDocumentFragment\u3002XmlDocument\u7c7b\u4ee3\u8868\u4e86\u4e00\u4e2aXML\u6587\u6863\uff0c\u5b83\u63d0\u4f9b\u4e86\u8f7d\u5165\u548c\u4fdd\u5b58XML\u6587\u6863\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\u3002\u8fd9\u4e9b\u65b9\u6cd5\u5305\u62ec\u4e86Load\u3001LoadXml\u548cSave\u7b49\u3002\u540c\u65f6\uff0c\u5b83\u8fd8\u63d0\u4f9b\u4e86\u6dfb\u52a0\u7279\u6027\uff08Attributes\uff09\u3001\u8bf4\u660e\uff08Comments\uff09\u3001\u7a7a\u95f4\uff08Spaces\uff09\u3001\u5143\u7d20\uff08Elements\uff09\u548c\u65b0\u8282\u70b9\uff08New Nodes\uff09\u7b49XML\u9879\u7684\u529f\u80fd\u3002XmlDocumentFragment\u7c7b\u4ee3\u8868\u4e86\u4e00\u90e8\u5206XML\u6587\u6863\uff0c\u5b83\u80fd\u88ab\u7528\u6765\u6dfb\u52a0\u5230\u5176\u4ed6\u7684XML\u6587\u6863\u4e2d\u3002 XmlDataDocument\u7c7b\u53ef\u4ee5\u8ba9\u7a0b\u5e8f\u5458\u66f4\u597d\u5730\u5b8c\u6210\u548cADO.NET\u4e2d\u7684\u6570\u636e\u96c6\u5bf9\u8c61\u4e4b\u95f4\u7684\u4e92\u64cd\u4f5c\u3002<br \/>\n\u9664\u4e86\u4e0a\u9762\u4ecb\u7ecd\u7684System.Xml\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7c7b\u5916\uff0c\u8be5\u547d\u540d\u7a7a\u95f4\u8fd8\u5305\u62ec\u4e86XmlConvert\u3001XmlLinkedNode\u4ee5\u53caXmlNodeList\u7b49\u7c7b\uff0c\u4e0d\u8fc7\u8fd9\u4e9b\u7c7b\u4e0d\u662f\u672c\u6587\u4ecb\u7ecd\u7684\u91cd\u70b9\uff0c\u6709\u5174\u8da3\u7684\u8bfb\u8005\u4e0d\u59a8\u53bb\u53c2\u8003\u76f8\u5173\u6587\u6863\u8d44\u6599\u3002<br \/>\nSystem.Xml.Schema\u547d\u540d\u7a7a\u95f4\u4e2d\u5305\u542b\u4e86\u548cXML\u6a21\u5f0f\u76f8\u5173\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u5305\u62ecXmlSchema\u3001XmlSchemaAll\u3001XmlSchemaXPath\u4ee5\u53caXmlSchemaType\u7b49\u7c7b\u3002<br \/>\nSystem.Xml.Serialization\u547d\u540d\u7a7a\u95f4\u4e2d\u5305\u542b\u4e86\u548cXML\u6587\u6863\u7684\u5e8f\u5217\u5316\u548c\u53cd\u5e8f\u5217\u5316\u64cd\u4f5c\u76f8\u5173\u7684\u7c7b\uff0cXML\u6587\u6863\u7684\u5e8f\u5217\u5316\u64cd\u4f5c\u80fd\u5c06XML\u683c\u5f0f\u7684\u6570\u636e\u8f6c\u5316\u4e3a\u6d41\u683c\u5f0f\u7684\u6570\u636e\u5e76\u80fd\u5728\u7f51\u7edc\u4e2d\u4f20\u8f93\uff0c\u800c\u53cd\u5e8f\u5217\u5316\u5219\u5b8c\u6210\u76f8\u53cd\u7684\u64cd\u4f5c\uff0c\u5373\u5c06\u6d41\u683c\u5f0f\u7684\u6570\u636e\u8fd8\u539f\u6210XML\u683c\u5f0f\u7684\u6570\u636e\u3002<br \/>\nSystem.Xml.XPath\u547d\u540d\u7a7a\u95f4\u5305\u542b\u4e86XPathDocument\u3001 XPathExression\u3001XPathNavigator\u4ee5\u53caXPathNodeIterator\u7b49\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u80fd\u5b8c\u6210XML\u6587\u6863\u7684\u5bfc\u822a\u529f\u80fd\u3002\u5728 XPathDocument\u7c7b\u7684\u534f\u52a9\u4e0b\uff0cXPathNavigator\u7c7b\u80fd\u5b8c\u6210\u5feb\u901f\u7684XML\u6587\u6863\u5bfc\u822a\u529f\u80fd\uff0c\u8be5\u7c7b\u4e3a\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e86\u8bb8\u591aMove\u65b9\u6cd5\u4ee5\u5b8c\u6210\u5bfc\u822a\u529f\u80fd\u3002<br \/>\nSystem.Xml.Xsl\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u7c7b\u5b8c\u6210\u4e86XSLT\u7684\u8f6c\u6362\u529f\u80fd\u3002<\/p>\n<p>\u4e09\uff0e\u8bfbXML\u6587\u6863\u7684\u65b9\u6cd5\uff1a<br \/>\n\u5728\u4ecb\u7ecd\u5b8c.Net\u6846\u67b6\u4e2d\u548cXML\u6709\u5173\u7684\u547d\u540d\u7a7a\u95f4\u548c\u76f8\u5173\u7c7b\u540e\uff0c\u6211\u63a5\u7740\u5411\u5927\u5bb6\u4ecb\u7ecd\u548cXML\u76f8\u5173\u7684\u4e00\u4e9b\u64cd\u4f5c\u3002\u9996\u5148\uff0c\u6211\u5411\u5927\u5bb6\u4ecb\u7ecd\u7684\u8bfb\u53d6XML\u6587\u6863\u7684\u65b9\u6cd5\u3002\u5728\u4e0b\u9762\u7684\u5b9e\u4f8b\u7a0b\u5e8f\u4e2d\uff0c\u6211\u5c06\u8fd0\u7528VS.net\u5f00\u53d1\u5de5\u5177\u9644\u5e26\u7684&#8221;books.xml&#8221;\u6587\u4ef6\u6765\u4f5c\u4e3a\u793a\u4f8b\u3002\u4f60\u53ef\u4ee5\u5728\u4f60\u7684\u673a\u5668\u4e0a\u641c\u7d22\u5230\u8be5\u6587\u4ef6\uff08\u6216\u8bf7\u53c2\u8003\u9644\u5f55\uff09\uff0c\u6216\u8005\u4f60\u4e5f\u53ef\u4ee5\u8fd0\u7528\u5176\u4ed6\u7684XML\u6587\u4ef6\u3002<br \/>\n\u9996\u5148\uff0c\u6211\u4eec\u7528XmlTextReader\u7c7b\u7684\u5bf9\u8c61\u6765\u8bfb\u53d6\u8be5XML\u6587\u6863\u3002\u65b9\u6cd5\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u5728\u521b\u5efa\u65b0\u5bf9\u8c61\u7684\u6784\u9020\u51fd\u6570\u4e2d\u6307\u660eXML\u6587\u4ef6\u7684\u4f4d\u7f6e\u5373\u53ef\u3002<br \/>\nXmlTextReader textReader = new XmlTextReader(&#8220;C:\\\\books.xml&#8221;);<br \/>\n\u4e00\u65e6\u65b0\u5bf9\u8c61\u521b\u5efa\u5b8c\u6bd5\uff0c\u4f60\u5c31\u53ef\u4ee5\u8c03\u7528\u5176Read\u65b9\u6cd5\u6765\u8bfb\u53d6XML\u6587\u6863\u4e86\u3002\u8c03\u7528Read\u65b9\u6cd5\u4e4b\u540e\uff0c\u4fe1\u606f\u88ab\u5b58\u50a8\u8d77\u6765\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u8bfb\u53d6\u8be5\u5bf9\u8c61\u7684Name\u3001BaseURI\u3001Depth\u3001LineNumber\u7b49\u5c5e\u6027\u6765\u83b7\u53d6\u8fd9\u4e9b\u4fe1\u606f\u3002\u4e0b\u9762\u6211\u7ed9\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u5b9e\u4f8b\uff0c\u8be5\u5b9e\u4f8b\u901a\u8fc7\u7b80\u5355\u7684\u8bfb\u53d6&#8221;books.xml&#8221;\u6587\u4ef6\uff0c\u7136\u540e\u5c06\u5176\u4e2d\u7684\u4fe1\u606f\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e2d\u3002 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System; \r\nusing System.Xml;   \r\nnamespace ReadXml \r\n{ \r\n    class Class1 \r\n    { \r\n        static void Main( string&#x5B;] args ) \r\n        { \r\n            \/\/ \u521b\u5efa\u4e00\u4e2aXmlTextReader\u7c7b\u7684\u5bf9\u8c61\u5e76\u8c03\u7528Read\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6 \r\n            XmlTextReader textReader = new XmlTextReader(&quot;C:\\\\books.xml&quot;); \r\n            textReader.Read(); \r\n            \/\/ \u8282\u70b9\u975e\u7a7a\u5219\u6267\u884c\u5faa\u73af\u4f53 \r\n            while ( textReader.Read() ) \r\n            { \r\n                \/\/ \u8bfb\u53d6\u7b2c\u4e00\u4e2a\u5143\u7d20 \r\n                textReader.MoveToElement(); \r\n                Console.WriteLine(&quot;XmlTextReader Properties Test&quot;); \r\n                Console.WriteLine(&quot;===================&quot;);   \r\n                \/\/ \u8bfb\u53d6\u8be5\u5143\u7d20\u7684\u5c5e\u6027\u5e76\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e2d \r\n                Console.WriteLine(&quot;Name:&quot; + textReader.Name); \r\n                Console.WriteLine(&quot;Base URI:&quot; + textReader.BaseURI); \r\n                Console.WriteLine(&quot;Local Name:&quot; + textReader.LocalName); \r\n                Console.WriteLine(&quot;Attribute Count:&quot; + textReader.AttributeCount.ToString()); \r\n                Console.WriteLine(&quot;Depth:&quot; + textReader.Depth.ToString()); \r\n                Console.WriteLine(&quot;Line Number:&quot; + textReader.LineNumber.ToString()); \r\n                Console.WriteLine(&quot;Node Type:&quot; + textReader.NodeType.ToString()); \r\n                Console.WriteLine(&quot;Attribute Count:&quot; + textReader.Value.ToString()); \r\n            } \r\n        } \r\n    } \r\n}\r\n<\/pre>\n<p>XmlTextReader\u7c7b\u4e2d\u6709\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u5c5e\u6027\uff0dNodeType\uff0c\u901a\u8fc7\u8be5\u5c5e\u6027\uff0c\u6211\u4eec\u53ef\u4ee5\u77e5\u9053\u5176\u8282\u70b9\u7684\u8282\u70b9\u7c7b\u578b\u3002\u800c\u679a\u4e3e\u7c7b\u578bXmlNodeType\u4e2d\u5305\u542b\u4e86\u8bf8\u5982Attribute\u3001CDATA\u3001Element\u3001Comment\u3001 Document\u3001DocumentType\u3001Entity\u3001ProcessInstruction\u4ee5\u53caWhiteSpace\u7b49\u7684XML\u9879\u7684\u7c7b\u578b\u3002\u901a\u8fc7\u4e0e XmlNodeType\u4e2d\u7684\u5143\u7d20\u7684\u6bd4\u8f83\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u53d6\u76f8\u5e94\u8282\u70b9\u7684\u8282\u70b9\u7c7b\u578b\u5e76\u5bf9\u5176\u5b8c\u6210\u76f8\u5173\u7684\u64cd\u4f5c\u3002\u4e0b\u9762\u6211\u5c31\u7ed9\u51fa\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u8be5\u5b9e\u4f8b\u8bfb\u53d6\u6bcf\u4e2a\u8282\u70b9\u7684 NodeType\uff0c\u5e76\u6839\u636e\u5176\u8282\u70b9\u7c7b\u578b\u663e\u793a\u5176\u4e2d\u7684\u5185\u5bb9\uff0c\u540c\u65f6\u7a0b\u5e8f\u8fd8\u8bb0\u5f55\u4e86XML\u6587\u4ef6\u4e2d\u6bcf\u79cd\u8282\u70b9\u7c7b\u578b\u7684\u6570\u76ee\u3002 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System; \r\nusing System.Xml;   \r\nnamespace ReadXML \r\n{ \r\n    class Class2 \r\n    { \r\n        static void Main( string&#x5B;] args ) \r\n        { \r\n            int ws = 0; \r\n            int pi = 0; \r\n            int dc = 0; \r\n            int cc = 0; \r\n            int ac = 0; \r\n            int et = 0; \r\n            int el = 0; \r\n            int xd = 0;   \r\n            XmlTextReader textReader = new XmlTextReader(&quot;C:\\\\books.xml&quot;);   \r\n            while (textReader.Read()) \r\n            { \r\n                XmlNodeType nType = textReader.NodeType; \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aXmlDeclaration \r\n                if (nType == XmlNodeType.XmlDeclaration) \r\n                { \r\n                    Console.WriteLine(&quot;Declaration:&quot; + textReader.Name.ToString()); \r\n                    xd = xd + 1; \r\n                }            \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aComment \r\n                if( nType == XmlNodeType.Comment) \r\n                { \r\n                    Console.WriteLine(&quot;Comment:&quot; + textReader.Name.ToString()); \r\n                    cc = cc + 1; \r\n                }            \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aAttribute \r\n                if( nType == XmlNodeType.Attribute) \r\n                { \r\n                    Console.WriteLine(&quot;Attribute:&quot; + textReader.Name.ToString()); \r\n                    ac = ac + 1; \r\n                }   \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aElement \r\n                if ( nType == XmlNodeType.Element) \r\n                { \r\n                    Console.WriteLine(&quot;Element:&quot; + textReader.Name.ToString()); \r\n                    el = el + 1; \r\n                }   \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aEntity \r\n                if ( nType == XmlNodeType.Entity ) \r\n                { \r\n                    Console.WriteLine(&quot;Entity:&quot; + textReader.Name.ToString()); \r\n                    et = et + 1; \r\n                }            \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aProcess Instruction \r\n                if( nType == XmlNodeType.ProcessingInstruction ) \r\n                { \r\n                    Console.WriteLine(&quot;Process Instruction:&quot; + textReader.Name.ToString()); \r\n                    pi = pi + 1; \r\n                }        \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aDocumentType \r\n                if( nType == XmlNodeType.DocumentType) \r\n                { \r\n                    Console.WriteLine(&quot;DocumentType:&quot; + textReader.Name.ToString()); \r\n                    dc = dc + 1; \r\n                }   \r\n                \/\/ \u8282\u70b9\u7c7b\u578b\u4e3aWhitespace \r\n                if ( nType == XmlNodeType.Whitespace ) \r\n                { \r\n                    Console.WriteLine(&quot;WhiteSpace:&quot; + textReader.Name.ToString()); \r\n                    ws = ws + 1; \r\n                } \r\n            }   \r\n            \/\/ \u5728\u63a7\u5236\u53f0\u4e2d\u663e\u793a\u6bcf\u79cd\u7c7b\u578b\u7684\u6570\u76ee \r\n            Console.WriteLine(&quot;Total Comments:&quot; + cc.ToString()); \r\n            Console.WriteLine(&quot;Total Attributes:&quot; + ac.ToString()); \r\n            Console.WriteLine(&quot;Total Elements:&quot; + el.ToString()); \r\n            Console.WriteLine(&quot;Total Entity:&quot; + et.ToString()); \r\n            Console.WriteLine(&quot;Total Process Instructions:&quot; + pi.ToString()); \r\n            Console.WriteLine(&quot;Total Declaration:&quot; + xd.ToString()); \r\n            Console.WriteLine(&quot;Total DocumentType:&quot; + dc.ToString()); \r\n            Console.WriteLine(&quot;Total WhiteSpaces:&quot; + ws.ToString()); \r\n        } \r\n    } \r\n}\r\n<\/pre>\n<p>\u4ee5\u4e0a\uff0c\u6211\u5411\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5982\u4f55\u8fd0\u7528XmlTextReader\u7c7b\u7684\u5bf9\u8c61\u6765\u8bfb\u53d6XML\u6587\u6863\uff0c\u5e76\u6839\u636e\u8282\u70b9\u7684NodeType\u5c5e\u6027\u6765\u53d6\u5f97\u5176\u8282\u70b9\u7c7b\u578b\u4fe1\u606f\u3002\u540c\u65f6XmlReader\u8fd9\u4e2a\u57fa\u7c7b\u8fd8\u6709XmlNodeReader\u548c XmlValidatingReader\u7b49\u6d3e\u751f\u7c7b\uff0c\u5b83\u4eec\u5206\u522b\u662f\u7528\u6765\u8bfb\u53d6XML\u6587\u6863\u7684\u8282\u70b9\u548c\u6a21\u5f0f\u7684\u3002\u9650\u4e8e\u7bc7\u5e45\uff0c\u8fd9\u91cc\u5c31\u4e0d\u4ecb\u7ecd\u4e86\uff0c\u8bfb\u8005\u53ef\u4ee5\u53c2\u8003\u6709\u5173\u8d44\u6599\u3002 <\/p>\n<p>\u56db\uff0e\u5199XML\u6587\u6863\u7684\u65b9\u6cd5\uff1a<br \/>\nXmlWriter\u7c7b\u5305\u542b\u4e86\u5199XML\u6587\u6863\u6240\u9700\u7684\u65b9\u6cd5\u548c\u5c5e\u6027\uff0c\u5b83\u662fXmlTextWriter\u7c7b\u548c XmlNodeWriter\u7c7b\u7684\u57fa\u7c7b\u3002\u8be5\u7c7b\u5305\u542b\u4e86WriteNode\u3001WriteString\u3001WriteAttributes\u3001 WriteStartElement\u4ee5\u53caWriteEndElement\u7b49\u4e00\u7cfb\u5217\u5199XML\u6587\u6863\u7684\u65b9\u6cd5\uff0c\u5176\u4e2d\u6709\u4e9b\u65b9\u6cd5\u662f\u6210\u5bf9\u51fa\u73b0\u7684\u3002\u6bd4\u5982\u4f60\u8981\u5199\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u4f60\u9996\u5148\u5f97\u8c03\u7528WriteStartElement\u65b9\u6cd5\uff0c\u63a5\u7740\u5199\u5165\u5b9e\u9645\u5185\u5bb9\uff0c\u6700\u540e\u662f\u8c03\u7528WriteEndElement\u65b9\u6cd5\u4ee5\u8868\u793a\u7ed3\u675f\u3002\u8be5\u7c7b\u8fd8\u5305\u542b\u4e86 WriteState\u3001XmlLang\u548cXmlSpace\u7b49\u5c5e\u6027\uff0c\u5176\u4e2dWriteState\u5c5e\u6027\u8868\u660e\u4e86\u5199\u7684\u72b6\u6001\u3002\u56e0\u4e3aXmlWriter\u7c7b\u5305\u542b\u4e86\u5f88\u591a\u5199 XML\u6587\u6863\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u8fd9\u91cc\u53ea\u662f\u4ecb\u7ecd\u6700\u4e3b\u8981\u7684\u51e0\u79cd\u3002\u4e0b\u9762\u6211\u4eec\u901a\u8fc7\u5176\u5b50\u7c7bXmlTextWriter\u7c7b\u6765\u8bf4\u660e\u5982\u4f55\u5199XML\u6587\u6863\u3002<br \/>\n\u9996\u5148\uff0c\u6211\u4eec\u8981\u521b\u5efa\u4e00\u4e2aXmlTextWriter\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61\u3002\u8be5\u7c7b\u7684\u6784\u9020\u51fd\u6570 XmlTextWriter\u6709\u4e09\u79cd\u91cd\u8f7d\u5f62\u5f0f\uff0c\u5176\u53c2\u6570\u5206\u522b\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u3001\u4e00\u4e2a\u6d41\u5bf9\u8c61\u548c\u4e00\u4e2aTextWriter\u5bf9\u8c61\u3002\u8fd9\u91cc\u6211\u4eec\u8fd0\u7528\u5b57\u7b26\u4e32\u7684\u53c2\u6570\u5f62\u5f0f\uff0c\u8be5\u5b57\u7b26\u4e32\u5c31\u6307\u660e\u4e86\u6240\u8981\u521b\u5efa\u7684XML\u6587\u4ef6\u7684\u4f4d\u7f6e\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a <\/p>\n<p>XmlTextWriter textWriter = New XmlTextWriter(&#8220;C:\\\\myXmFile.xml&#8221;, null);<\/p>\n<p>\u5728\u521b\u5efa\u5b8c\u5bf9\u8c61\u540e\uff0c\u6211\u4eec\u8c03\u7528WriterStartDocument\u65b9\u6cd5\u5f00\u59cb\u5199XML\u6587\u6863\uff0c\u5728\u5b8c\u6210\u5199\u5de5\u4f5c\u540e\uff0c\u5c31\u8c03\u7528WriteEndDocument\u7ed3\u675f\u5199\u8fc7\u7a0b\u5e76\u8c03\u7528Close\u65b9\u6cd5\u5c06\u5b83\u5173\u95ed\u3002\u5728\u5199\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u8c03\u7528WriteComment\u65b9\u6cd5\u6765\u6dfb\u52a0\u8bf4\u660e\uff0c\u901a\u8fc7\u8c03\u7528WriteString\u65b9\u6cd5\u6765\u6dfb\u52a0\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u901a\u8fc7\u8c03\u7528WriteStartElement\u548cWriteEndElement\u65b9\u6cd5\u5bf9\u6765\u6dfb\u52a0\u4e00\u4e2a\u5143\u7d20\uff0c\u901a\u8fc7\u8c03\u7528WriteStartAttribute\u548cWriteEndAttribute\u65b9\u6cd5\u5bf9\u6765\u6dfb\u52a0\u4e00\u4e2a\u5c5e\u6027\u3002\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528 WriteNode\u65b9\u6cd5\u6765\u6dfb\u52a0\u6574\u4e00\u4e2a\u8282\u70b9\uff0c\u5176\u5b83\u7684\u5199\u7684\u65b9\u6cd5\u8fd8\u5305\u62ecWriteProcessingInstruction\u548cWriteDocType\u7b49\u7b49\u3002\u4e0b\u9762\u7684\u5b9e\u4f8b\u5c31\u662f\u4ecb\u7ecd\u5982\u4f55\u5177\u4f53\u8fd0\u7528\u8fd9\u4e9b\u65b9\u6cd5\u6765\u5b8c\u6210XML\u6587\u6863\u7684\u5199\u5de5\u4f5c\u7684\u3002 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System; \r\nusing System.Xml;   \r\nnamespace WriteXML \r\n{ \r\nclass Class1 \r\n{ \r\nstatic void Main( string&#x5B;] args ) \r\n{ \r\n   try \r\n   { \r\n    \/\/ \u521b\u5efaXmlTextWriter\u7c7b\u7684\u5b9e\u4f8b\u5bf9\u8c61 \r\n    XmlTextWriter textWriter = new XmlTextWriter(&quot;C:\\\\w3sky.xml&quot;, null); \r\n    textWriter.Formatting = Formatting.Indented; \r\n    \/\/ \u5f00\u59cb\u5199\u8fc7\u7a0b\uff0c\u8c03\u7528WriteStartDocument\u65b9\u6cd5 \r\n    textWriter.WriteStartDocument();   \r\n    \/\/ \u5199\u5165\u8bf4\u660e \r\n    textWriter.WriteComment(&quot;First Comment XmlTextWriter Sample Example&quot;); \r\n    textWriter.WriteComment(&quot;w3sky.xml in root dir&quot;);    \r\n    \/\/\u521b\u5efa\u4e00\u4e2a\u8282\u70b9 \r\n    textWriter.WriteStartElement(&quot;Administrator&quot;); \r\n    textWriter.WriteElementString(&quot;Name&quot;, &quot;formble&quot;); \r\n    textWriter.WriteElementString(&quot;site&quot;, &quot;w3sky.com&quot;); \r\n    textWriter.WriteEndElement();\r\n\r\n    \/\/ \u5199\u6587\u6863\u7ed3\u675f\uff0c\u8c03\u7528WriteEndDocument\u65b9\u6cd5 \r\n    textWriter.WriteEndDocument(); \r\n    \/\/ \u5173\u95edtextWriter \r\n    textWriter.Close(); \r\n   } \r\n   catch(System.Exception e) \r\n   { \r\n    Console.WriteLine(e.ToString()); \r\n   } \r\n} \r\n} \r\n}\r\n<\/pre>\n<p>\u4e94\uff0e\u8fd0\u7528XmlDocument\u7c7b\uff1a<br \/>\nXmlDocument\u7c7b\u7684\u5bf9\u8c61\u4ee3\u8868\u4e86\u4e00\u4e2aXML\u6587\u6863\uff0c\u5b83\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u91cd\u8981\u7684XML\u7c7b\u3002\u8be5\u7c7b\u5305\u542b\u4e86 Load\u3001LoadXml\u4ee5\u53caSave\u7b49\u91cd\u8981\u7684\u65b9\u6cd5\u3002\u5176\u4e2dLoad\u65b9\u6cd5\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5b57\u7b26\u4e32\u6307\u5b9a\u7684XML\u6587\u4ef6\u6216\u662f\u4e00\u4e2a\u6d41\u5bf9\u8c61\u3001\u4e00\u4e2aTextReader\u5bf9\u8c61\u3001\u4e00\u4e2aXmlReader\u5bf9\u8c61\u5bfc\u5165XML\u6570\u636e\u3002LoadXml\u65b9\u6cd5\u5219\u5b8c\u6210\u4ece\u4e00\u4e2a\u7279\u5b9a\u7684XML\u6587\u4ef6\u5bfc\u5165XML\u6570\u636e\u7684\u529f\u80fd\u3002\u5b83\u7684Save\u65b9\u6cd5\u5219\u5c06XML\u6570\u636e\u4fdd\u5b58\u5230\u4e00\u4e2aXML\u6587\u4ef6\u4e2d\u6216\u662f\u4e00\u4e2a\u6d41\u5bf9\u8c61\u3001\u4e00\u4e2aTextWriter\u5bf9\u8c61\u3001\u4e00\u4e2aXmlWriter\u5bf9\u8c61\u4e2d\u3002<br \/>\n\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\u6211\u4eec\u7528\u5230\u4e86XmlDocument\u7c7b\u5bf9\u8c61\u7684LoadXml\u65b9\u6cd5\uff0c\u5b83\u4ece\u4e00\u4e2aXML\u6587\u6863\u6bb5\u4e2d\u8bfb\u53d6XML\u6570\u636e\u5e76\u8c03\u7528\u5176Save\u65b9\u6cd5\u5c06\u6570\u636e\u4fdd\u5b58\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ \u521b\u5efa\u4e00\u4e2aXmlDocument\u7c7b\u7684\u5bf9\u8c61 \r\nXmlDocument doc = new XmlDocument(); \r\ndoc.LoadXml((&quot;&lt;Student type=\u2019regular\u2019 Section=\u2019B\u2019&gt;&lt;Name&gt;Tommy Lex&lt;\/Name&gt;&lt;\/Student&gt;&quot;)); \r\n\/\/ \u4fdd\u5b58\u5230\u6587\u4ef6\u4e2d \r\ndoc.Save(&quot;C:\\\\student.xml&quot;);\r\n<\/pre>\n<p>\u8fd9\u91cc\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8Save\u65b9\u6cd5\u4e2d\u53c2\u6570\uff0c\u5c06XML\u6570\u636e\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e2d\uff0c\u65b9\u6cd5\u5982\u4e0b\uff1a <\/p>\n<p>doc.Save(Console.Out);<\/p>\n<p>\u800c\u5728\u4e0b\u9762\u7684\u7a0b\u5e8f\u4e2d\uff0c\u6211\u4eec\u7528\u5230\u4e86\u4e00\u4e2aXmlTextReader\u5bf9\u8c61\uff0c\u901a\u8fc7\u5b83\u6211\u4eec\u8bfb\u53d6 &#8220;books.xml&#8221;\u6587\u4ef6\u4e2d\u7684XML\u6570\u636e\u3002\u7136\u540e\u521b\u5efa\u4e00\u4e2aXmlDocument\u5bf9\u8c61\u5e76\u8f7d\u5165XmlTextReader\u5bf9\u8c61\uff0c\u8fd9\u6837XML\u6570\u636e\u5c31\u88ab\u8bfb\u5230 XmlDocument\u5bf9\u8c61\u4e2d\u4e86\u3002\u6700\u540e\uff0c\u901a\u8fc7\u8be5\u5bf9\u8c61\u7684Save\u65b9\u6cd5\u5c06XML\u6570\u636e\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e2d\u3002 <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nXmlDocument doc = new XmlDocument(); \r\n\/\/ \u521b\u5efa\u4e00\u4e2aXmlTextReader\u5bf9\u8c61\uff0c\u8bfb\u53d6XML\u6570\u636e \r\nXmlTextReader reader = new XmlTextReader(&quot;c:\\\\books.xml&quot;); \r\nreader.Read(); \r\n\/\/ \u8f7d\u5165XmlTextReader\u7c7b\u7684\u5bf9\u8c61 \r\ndoc.Load(reader); \r\n\/\/ \u5c06XML\u6570\u636e\u663e\u793a\u5728\u63a7\u5236\u53f0\u4e2d \r\ndoc.Save(Console.Out);\r\n<\/pre>\n<p>\u516d\uff0e\u603b\u7ed3\uff1a<br \/>\nXML\u6280\u672f\u4f5c\u4e3a.Net\u7684\u57fa\u77f3\uff0c\u5176\u91cd\u8981\u6027\u81ea\u7136\u4e0d\u8a00\u800c\u55bb\u3002.Net\u6846\u67b6\u5305\u542b\u4e86\u4e94\u4e2a\u547d\u540d\u7a7a\u95f4\u548c\u5927\u91cf\u7684\u7c7b\u6765\u652f\u6301\u4e0eXML\u6280\u672f\u6709\u5173\u7684\u64cd\u4f5c\u3002\u5176\u4e2dSystem.Xml\u662f\u6700\u91cd\u8981\u7684\u4e00\u4e2a\u547d\u540d\u7a7a\u95f4\uff0c\u5176\u4e2d\u7684XmlReader\u7c7b\u548cXmlWriter\u7c7b\u4ee5\u53ca\u5b83\u4eec\u7684\u6d3e\u751f\u7c7b\u5b8c\u6210\u4e86 XML\u6587\u6863\u7684\u8bfb\u5199\u64cd\u4f5c\uff0c\u662f\u6700\u57fa\u672c\u4e5f\u662f\u6700\u91cd\u8981\u7684\u7c7b\u3002XmlDocument\u7c7b\u4ee3\u8868\u4e86XML\u6587\u6863\uff0c\u5b83\u80fd\u5b8c\u6210\u4e0e\u6574\u4e2aXML\u6587\u6863\u76f8\u5173\u7684\u5404\u7c7b\u64cd\u4f5c\uff0c\u540c\u65f6\u548c\u5176\u76f8\u5173\u7684 XmlDataDocument\u7c7b\u4e5f\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u503c\u5f97\u8bfb\u8005\u7684\u6df1\u5165\u7814\u7a76\u3002<br \/>\n\u9644\u5f55<br \/>\n&#8220;books.xml&#8221;\u6587\u4ef6\u5982\u4e0b\uff1a <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&lt;?xml version=\u20191.0\u2019?&gt; \r\n&lt;!-- This file represents a fragment of a book store inventory database --&gt; \r\n&lt;bookstore&gt; \r\n&lt;book genre=&quot;autobiography&quot; publicationdate=&quot;1981&quot; ISBN=&quot;1-861003-11-0&quot;&gt; \r\n    &lt;title&gt;The Autobiography of Benjamin Franklin&lt;\/title&gt; \r\n    &lt;author&gt; \r\n      &lt;first-name&gt;Benjamin&lt;\/first-name&gt; \r\n      &lt;last-name&gt;Franklin&lt;\/last-name&gt; \r\n    &lt;\/author&gt; \r\n    &lt;price&gt;8.99&lt;\/price&gt; \r\n&lt;\/book&gt; \r\n&lt;book genre=&quot;novel&quot; publicationdate=&quot;1967&quot; ISBN=&quot;0-201-63361-2&quot;&gt; \r\n    &lt;title&gt;The Confidence Man&lt;\/title&gt; \r\n    &lt;author&gt; \r\n      &lt;first-name&gt;Herman&lt;\/first-name&gt; \r\n      &lt;last-name&gt;Melville&lt;\/last-name&gt; \r\n    &lt;\/author&gt; \r\n    &lt;price&gt;11.99&lt;\/price&gt; \r\n&lt;\/book&gt; \r\n&lt;book genre=&quot;philosophy&quot; publicationdate=&quot;1991&quot; ISBN=&quot;1-861001-57-6&quot;&gt; \r\n    &lt;title&gt;The Gorgias&lt;\/title&gt; \r\n    &lt;author&gt; \r\n      &lt;first-name&gt;Sidas&lt;\/first-name&gt; \r\n      &lt;last-name&gt;Plato&lt;\/last-name&gt; \r\n    &lt;\/author&gt; \r\n    &lt;price&gt;9.99&lt;\/price&gt; \r\n&lt;\/book&gt; \r\n&lt;\/bookstore&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\uff0e\u524d\u8a00\uff1a XML\u662f\u5fae\u8f6f.Net\u6218\u7565\u7684\u4e00\u4e2a\u91cd\u8981\u7ec4\u6210\u90e8\u5206\uff0c\u800c\u4e14\u5b83\u53ef\u8c13\u662fXML Web\u670d\u52a1\u7684\u57fa\u77f3\uff0c\u6240\u4ee5\u638c\u63e1.Net [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-code_related"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":4625,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions\/4625"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}