Detailed introduction to code sharing using xsd to verify xml

黄舟
Release: 2017-03-23 16:55:10
Original
2155 people have browsed it

Xsd

XMLStructure Definition(XML Schemas Definition)
XML Schema
is a replacement forDTD.XML SchemaThe language is alsoXSD.
XML Schema
describes the structure of theXMLdocument. You can validate aXMLdocument against a specifiedXML Schemato check thatXMLWhether the document meets its requirements.
Document designers can specify the allowed structure and content of an
XMLdocument throughXML Schema, and can check accordingly Whether anXMLdocument is valid.XML Schemaitself is aXMLdocument, which conforms to theXMLsyntax structure.
It can be parsed with the general
XMLparser.
AXML Schemawill define: elements that appear in the document, attributes that appear in the document, sub-elements, sub-elements The number of elements, the order of child elements, whether the element is empty, the data type of elements and attributes, the default and fixed values of elements or attributes.

The suffix name of the XSDfile is.xsd.

In the following code example, the above schema is added to theXmlReaderSettingsobject'sXmlSchemaSetSchemasproperty.XmlReaderSettingsTheValidationTypeproperty of the object is set toSchema
, forced to pass the
XmlReader
object'sCreateMethodValidationXMLDocument.AddValidationEventHandlerto theXmlReaderSettingsobject to handle anyWarning raised by errors found duringXMLdocument and schema validationorErrorevent.
Below is an example:


using System; using System.Xml; using System.Xml.Schema; using System.IO; using System.Xml.Serialization; using System.Text; public class XmlSchemaSetExample { static void Main() { XmlReaderSettings booksSettings = new XmlReaderSettings(); booksSettings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd"); booksSettings.ValidationType = ValidationType.Schema; booksSettings.ValidationEventHandler += new ValidationEventHandler(booksSettingsValidationEventHandler); MemoryStream ms = new MemoryStream();//定义一个数据流对象 XmlDocument doc = new XmlDocument(); doc.Load("contosoBooks.xml"); doc.Save(ms); ms.Position = 0; //修改指针的位置 XmlReader books = XmlReader.Create(ms,booksSettings); while (books.Read()) { } } static void booksSettingsValidationEventHandler(object sender, ValidationEventArgs e) { if (e.Severity == XmlSeverityType.Warning) { Console.Write("WARNING: "); Console.WriteLine(e.Message); Console.Read(); } else if (e.Severity == XmlSeverityType.Error) { Console.Write("ERROR: "); Console.WriteLine(e.Message); Console.Read(); } } }
Copy after login
contosoBooks.xsd

                            
Copy after login


contosoBooks.xml

##

   The Autobiography of Benjamin Franklin  Benjamin Franklin  8.99   The Confidence Man  Herman Melville  11.99   The Gorgias  Plato  9.99  
Copy after login


Remarks: About some other classes or instances of Xsd

StreamWriter,

StreamReader,XmlSchema,XmlSchemaSet

Stream stream = new MemoryStream(); //When an object of a class cannot be initialized, you can consider using itsinheritancekind.

## FileStream fs = File.Open("117.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);

123.xml");##     TextReader tr2= new StringReader("asdfsadfsdf");



The above is the detailed content of Detailed introduction to code sharing using xsd to verify xml. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!