XML概念 Root XML根节点,只能且必须有一个(以上为Link Library) Element 节点元素(如Link) Attribute 节点属性(如Cat, Url, Desc) Content 内容(非空白文本、CDATA、Element、End Element、Entity Reference 或 End Entity)节点 System. Create("2books.xml")) 挖到子节点 using (Xml Reader reader = Xml Reader. Write Start Document(); //预处理指示(Write the Processing Instruction node.) String PItext="type='text/xsl' href='book.xsl'"; writer. Close(); //Load the file into an Xml Document to ensure well formed XML. Xml空间 以下类适合快速流式读写XML文件(注:DOM适合随机读写) Xml Reader, Xml Writer, Xml Text Reader, Xml Text Writer Xml Validating Reader, Xml Validating Writer 添加了DTD和模式验证,因此提供了数据的有效性验证 Xml Node Reader, Xml Node Writer 把Xml Node作为其源 节点类型(public enum Xml Node Type) Xml Declaration XML 声明(例如,)。 Significant Whitespace 混合内容模型中标记间的空白或 xml:space="preserve" 范围内的空白。 Text 节点的文本内容。 Whitespace 标记间的空白。 ------------------------------------------------------------------------使用Xml Text Reader快速读取------------------------------------------------------------------------创建 打开 Xml Text Reader reader = new Xml Text Reader("*.xml"); 关闭 reader. Create("book3.xml")) 读到指定位置 File Stream fs = new File Stream("..\..\..\books.xml", File Mode. Write Processing Instruction("xml-stylesheet", PItext); //文档类型(Write the Document Type node.) writer. Write End Document(); //Write the XML to file and close the writer. Xml Document doc = new Xml Document(); //Preserve white space for readability. Get Elements By Tag Name("g Age"); Xml Node List girl Cell Phone Number = xml Doc. If you are after one specific element, you can access child elements with the indexer: xml Doc["Root"], and these can be chained: xml Doc["Root"]["Folder"]["Item"] to dig down the hierarchy (although it's sensible to validate that these elements actually exist) to create a object tree out of your XML and vice versa.


Load("your XMLFile.xml"); // Load the XML document from the specified file // Get elements Xml Node List girl Address = xml Doc. Write Line("Phone Number: " girl Cell Phone Number[0].

Get Elements By Tag Name("g Address"); Xml Node List girl Age = xml Doc. Inner Text); 1 for mentioning Xml Document, which is much more convenient than serialisation interfaces in some cases.

I'm not sure whether "best practice for parsing XML" exists.

There are numerous technologies suited for different situations. You can go with LINQ to XML, Thank you for your interest in this question.

@George Stocker the questions are different enough to co-exist and both have great answers, plus the accepted ones are using different technologies.

Last modified 09-Oct-2019 09:04