SoFunction
Updated on 2025-03-08

C# Implementation method to use xsd file to verify whether the XML format is correct

This article describes the implementation method of C# using xsd files to verify whether the XML format is correct. Share it for your reference, as follows:

//Create xmlDocumentXmlDocument doc = new XmlDocument();
//Create a declaration segment such as <?xml version="1.0" encoding="utf-8" ?>(("1.0", "utf-8", null));
//Create a root node KYTResultsXmlElement results = ("KYTResults");
//Create ResultsStatusXmlNode resultsStatus = ("ResultsStatus");
//Create LevelXmlElement element = ("Level");
 = status ? "0" : "1";
(element);
//Create Descriptionelement = ("Description");
 = msg;
(element);
//Create PassKey If the user login fails, empty characters will be loadedelement = ("PassKey");
 = key;
(element);
(resultsStatus);
//END Create ResultsStatus//Create DataList data collectionif (status)
{
  (dataList);
}
//END Create a root node KYTResults(results);
string path = ("/ws/xsd/");
//Verify whether the xml format is correct and pass xsd verificationstring error = "";
//Declare XmlSchemaXmlSchemaSet schemas = new XmlSchemaSet();
("", (path));
//Declare event handling methodValidationEventHandler eventHandler = new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) {
  switch ()
  {
   case :
    error += ;
    break;
   case :
    break;
  }
});
 = schemas;
//Verify xml(eventHandler);
// Check if there is any exception. If the format is incorrect, throw itif (!"".Equals(error))
{
  throw new Exception(error);
}

PS: Here are a few more practical XML-related online tools for everyone to use:

Online XML formatting/compression tools:
http://tools./code/xmlformat

Online XML/JSON mutual conversion tool:
http://tools./code/xmljson

XML online compression/formatting tools:
http://tools./code/xml_format_compress

XML code online formatting and beautification tool:
http://tools./code/xmlcodeformat

For more information about C# related content, please check out the topic of this site:Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of thread usage techniques for C# programming》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Summary of C# array operation skills"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.