Home  >  Article  >  Backend Development  >  Easily process XML data in .NET Framework (2-2)

Easily process XML data in .NET Framework (2-2)

黄舟
黄舟Original
2016-12-20 14:07:311199browse

??String and Fragment

??Programmers will find that there is a big difference between COM and .NET Framework XML API when they cut the program in MSXML. The .NET Framework classes themselves do not provide methods to parse XML data stored in strings. Unlike the MSXML parser object, the XmlTestReader class does not provide any LoadXML method to create a browser from a well-formed character. There is no method similar to LoadXML because you can use a special text reader---StringReader class to obtain the same function.

??One of the XmlTextReader constructor functions receives a TextReader derived object and an XML reader as parameters (the browser is created based on the content of the text reader). A text reader class is a stream that is generated by optimizing the input characters. The StringReader class extends the TextReader class and uses an in-memory string as its input stream. The following code snippet demonstrates how to initialize an XML reader with a well-formed XML string as its input:

??string xmlText = '...';

??StringReader strReader = new StringReader(xmlText) ;

??XmlTextReader reader = new XmlTextReader(strReader);



??In addition, using the StringWriter class instead of the TextWrite class, you can create an XML document from memory characters.

??An XML string of a specified type is an XML fragment. An XML fragment consists of XML text, but an XML document without a root node is not a well-formed XML document, so it cannot be exploited. An XML fragment is part of the original document, so it may lack a root node. For example, the following XML text is a valid XML fragment, but not a valid XML document because it has no root node:

??Dino

??Esposito

??The .NET Framework XML API allows programmers to XML fragments are used in conjunction with a parser content consisting of things like encoding character set, DTD document, namespace, language and whitespace handlers:

??public XmlTextReader(

??string xmlFragment,

?? XmlNodeType fragType,

??XmlParserContext context

??);

??xmlFragment parameter contains XML string analysis. The FragType parameter represents the type of fragment, which gives the type of the fragment root node. Only nodes of element, attibute and document types can be used as the root node of a fragment, and the content of the analyzer can be interpreted by the XmlParserContext class.


Please indicate the source when reprinting: Easily process XML data in .NET Framework (2-2)

The above is the content of Easily process XML data in .NET Framework (2-2), please pay attention to more related content PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn