Method for schema validation of XML documents (for Framework2.0 and above)

Source: Internet
Author: User

function function: The document is detected according to the schema of the existing document XML

Parameter meaning: Strarchxmlfilepath (document XML file path)

public static bool Checkarchxmlcontent (string strarchxmlfilepath, out string strerrormsg)

{

BOOL Bresult = true;

Strerrormsg = "";

Try

{

Initialize error message

M_strerrormsg = "";

String Strxsdfilepath = "Example.xsd"; XSD Schema document

Constructing the schema schema cache

XmlSchemaSet xssarchcontent = new XmlSchemaSet ();

Add a schema file, preceded by a namespace, is not empty

Xssarchcontent.add ("", Strxsdfilepath);

Define how the document mode is used

XmlReaderSettings xrsarchcontent = new XmlReaderSettings ();

Xrsarchcontent.validationtype = ValidationType.Schema;

Correlation validation Reader and schema collection

Xrsarchcontent.schemas = xssarchcontent;

To add an event handler when an error occurs

Xrsarchcontent.validationeventhandler + = new ValidationEventHandler (dealerror);

Use the latest way to build a reader that can be validated and construct a validation reader

XmlReader xrarchcontent = xmlreader.create (Strarchxmlfilepath, xrsarchcontent);

Cyclic detection of all document nodes

while (Xrarchcontent.read ())

{

}

Determine if a node has syntax errors

if (m_strerrormsg! = "")

{

Bresult = false;

Strerrormsg = m_strerrormsg;

}

}

catch (Exception e)

{

Strerrormsg = E.message.tostring ();

Bresult = false;

}

return bresult;

}

function function: Error handler

public static void Dealerror (object sender, ValidationEventArgs args)

{

Working with Content

M_strerrormsg + = args. Message.tostring () + "/r/n/r/n";

}

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.