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

Easily process XML data in .NET Framework (3-1)

黄舟
黄舟Original
2016-12-20 14:09:091305browse

??Browser with validation

??The XmlValidatingReader class implements the XmlReader class, which provides support for multiple types of XML validation: DTD, XML-Data Reduced (XDR) architecture, and XSD. DTD and XSD are W3C Officially recommended. XDR is a format used by Microsoft in its early days to handle XML architecture.

??You can use the XmlVlidatingReader class to validate XML documents and XML fragments. The XmlValidatingReader class works on XML browsers---it is a typical instance of the XMLTextReader class. XMLTextReade is used to read the nodes of the document, but XmlVlidatingReader validates each XML block according to the required validation type.

??The XmlVlidatingReader class only implements a very small subset of functions necessary for XML browsers. This class always works with an existing XML browser and supervises methods and properties. If you dig into the structure of this class, you'll find that it obviously depends on an existing text browser. XML browsers with validation cannot serialize directly from a file or a URL. The list of structure functions of this class is as follows:

public XmlValidatingReader(XmlReader);

public XmlValidatingReader(Stream, XmlNodeType, XmlParserContext);

public XmlValidatingReader(string, XmlNodeType, XmlParserContext); It can parse any XML fragment, which is supplied through a string or a stream, and it can also parse any XML document provided by the browser.

??There are very few methods that have major changes in the XmlVlidatingReader class (compared to other reader classes). In addition, for Read, it has Skip and ReadTypedValue methods. The Skip method skips all child nodes of the current node (you can't skip poorly formatted XML text, it is a very useful algorithm), and the Skip method also verifies the skipped content. The ReadTypedValue method returns the CLR type corresponding to the specified XML Schema (XSD) type. If this method finds the CLR type corresponding to the XSD type, it returns the CLR type name. If not found, the node's value is returned as a string value.

??The XML browser with validation is just as its name suggests, it is a node-based browser that verifies whether the structure of the current node conforms to the current schema. Validation is incremental; it has no method that returns a Boolean value indicating whether the document is valid. Usually you use the Read method to read the input XML document. In fact, you can also use a browser with authentication to read XML documents. At each step, whether the structure of the currently visited node matches the specified schema, if not, an exception is thrown. Figure 4 is a console application that has a command line to enter a file name and finally outputs the verification results.

Figure 4 Console App

using System;

using System.
{

try {

Validate(fileName);

}

catch (Exception e) {

Console.WriteLine('Error:t{0}', e.Message);

Console.WriteLine('Exception raised: { 0}',

e.GetType().ToString());

}

}



private void Validate(String fileName)

{

XmlTextReader xtr = new XmlTextReader(fileName);

XmlValidatingReader vreader = new XmlValidatingReader(xtr);

vreader.ValidationType = ValidationType.Auto; ;

vreader.MoveToContent ();



while (vreader.Read()) {}



xtr.Close();
ValidationEventArgs args)

{

Console.Write('Validation error: ' args.Message 'rn');

}



public static void Main(String[] args)

{

MyXmlValid App o = new MyXmlValidApp(args[0]);

return;

}

}



The above is the content of easily processing XML data (3-1) in .NET Framework. For more related content, please pay attention to 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