Verify the XML document in. Net code

Source: Internet
Author: User

When verifying whether an XML document conforms to a certain format, are you still using the code to read and judge a node, a property, and a property? The. NET Framework provides some useful infrastructure to help us complete this job.

Let's first list the infrastructure (the introduction is from msdn, so I am too lazy to translate it ):

1. xmlreader

Provides forward-only, read-only access to a stream of XML data. The xmlreader class conforms to the W3C Extensible Markup Language (XML) 1.0 and the namespaces in XML recommendations.
Although the Microsoft. net Framework uplodes concrete implementations of the xmlreader class, such as the xmltextreader, xmlnodereader, And the xmlvalidatingreader classes, In the 2.0 release the recommended practice is to create xmlreader instances using the create method. for more information, see creating XML readers.

2. xmlreadersettings
Specifies a set of features to support on the xmlreader object created by the create method.

Note the following when using xmlreadersettings:

1) The processinlineschema and processschemalocation validation flags of an xmlreadersettings object are not set by default. when these flags are set, the xmlresolver of the xmlreadersettings object is used to resolve schema locations encountered in the instance document in the xmlreader. if the xmlresolver object is a null reference (nothing in Visual Basic), schema locations are not resolved even if the processinlineschema and processschemalocation validation flags are set.
2) schemas added during validation add new types and can change the validation outcome of the document being validated. As a result, external schemas shoshould only be resolved from trusted sources.
3) validation error messages may expose sensitive content model information. validation Error and warning messages are handled using the validationeventhandler delegate, or are exposed as an xmlschemavalidationexception if no event handler is provided to the xmlreadersettings object (validation warnings do not cause an xmlschemavalidationexception to be thrown ). this content model information shoshould not be exposed in untrusted scenarios. validation warning messages are suppressed by default and can be reported by setting the reportvalidationwarnings flag.
4) The sourceuri property of an xmlschemavalidationexception returns the URI path to the schema file that caused the exception. The sourceuri property shocould not be exposed in untrusted scenarios.
5) disabling the specified flag (enabled by default) is recommended when validating, untrusted, large XML documents in High Availability scenarios against a schema with identity constraints over a large part of the document.
6) xmlreadersettings objects can contain in sensitive information such as user credentials. You shocould be careful when caching xmlreadersettings objects, or when passing the xmlreadersettings object from one component to another.
7) DTD processing is disabled by default. if you enable DTD processing, you need to be aware of including dtds from untrusted sources and possible denial of service attacks. use the xmlsecureresolver to restrict the resources that the xmlreader can access.
8) do not accept supporting components, such as nametable, xmlnamespacemanager, and xmlresolver objects, from an untrusted source.

After the infrastructure column is complete, how should we start this verification journey?

First, create an xmlreadersettings object and set the corresponding attributes in the xmlreadersettings object. We want to use schema to verify the read XML file and set the validationtype attribute of the object to a type, specify the verification event processor and add the schema file name and its namespace to the schema set:

Xmlreadersettings settings = new xmlreadersettings ();
Settings. validationtype = validationtype. Schema;
Settings. schemas. Add ("urn: Example-schema", "example. XSD ");
Settings. validationeventhandler + = new validationeventhandler (checkcallback );

Then, use the xmlreadersettings object as the parameter of the create method of the xmlreader class to obtain an xmlreader object.

Xmlreader reader = xmlreader. Create ("example_data.xml", settings );

In this way, when executing the read method, the xmlreader object will use the schema file we specified to verify the XML file it reads:

While (reader. Read ())
{
...
}

We put all the Code together:

Xmlreadersettings settings = new xmlreadersettings ();
Settings. validationtype = validationtype. Schema;
Settings. schemas. Add ("urn: Example-schema", "example. XSD ");
Settings. validationeventhandler + = new validationeventhandler (checkcallback );
Xmlreader reader = xmlreader. Create ("example_data.xml", settings );
Try
{
While (reader. Read ())
{
...
}
}
Finally
{
Reader. Close ();
}


Let's see, isn't it more concise to read and judge a node or attribute than a previous node?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.