Method One:
I use XmlReader to deal with it because I feel XmlReader simple point.
I have made an example to illustrate that the XML in the example is not very complex and has no child nodes.
XML file (a.xml):
<?XML version= "1.0" encoding= "Utf-8"?>< Profile> <profileID>0001</profileID> <desciption>My content</desciption> <URL><! [Cdata[http://www.bclc.com/app/DidYouWin/WinningNumbers/Lotto649Print.asp? Year=2000&month=6&draw_date=&gameid=1]]></URL> <NUMBERS>6</NUMBERS> <BONUS>True</BONUS></ Profile>
xsd file (a.xsd):
<?XML version= "1.0" encoding= "utf-16"?><Xsd:schemaattributeFormDefault= "unqualified"elementFormDefault= "qualified"version= "1.0"xmlns:xsd= "Http://www.w3.org/2001/XMLSchema"> <xsd:elementname= "Profile"> <Xsd:complextype> <xsd:sequence> <xsd:elementname= "profileID"type= "Xsd:int" /> <xsd:elementname= "Desciption"type= "Xsd:string" /> <xsd:elementname= "URL"type= "Xsd:string" /> <xsd:elementname= "NUMBERS"type= "Xsd:int" /> <xsd:elementname= "BONUS"type= "Xsd:boolean" /> </xsd:sequence> </Xsd:complextype> </xsd:element></Xsd:schema>
C # uses XSD files to verify that the XML format is correct:
XmlReaderSettings st =Newxmlreadersettings (); XmlSchemaSet Schemaset=NewXmlSchemaSet (); Schemaset.add (NULL, spath);//St. ValidationEventHandler + = new ValidationEventHandler (ValidationEventHandler);St. Validationflags = St. Validationflags |xmlschemavalidationflags.reportvalidationwarnings;st. ValidationType=validationtype.schema;st. Schemas.add (Schemaset,@"a.xsd"); //sets the event to validate XML when an error occurs. St. ValidationEventHandler + = (obj, e) =>{//this adds to your information processing. }; XmlReader XR = xmlreader.create (@"A.xml", ST); while(XR. Read ()) {if(XR. Isstartelement () {XR. Read (); }} XR. Close ();
Method Two:
//Create XmlDocumentXmlDocument doc =NewXmlDocument (); //create a declaration segment such as <?xml version= "1.0" encoding= "Utf-8"?>Doc. AppendChild (Doc. Createxmldeclaration ("1.0","Utf-8",NULL)); //Create a root node kytresultsXmlElement results = doc. CreateElement ("Kytresults"); //Create ResultsstatusXmlNode resultsstatus = doc. CreateElement ("Resultsstatus"); //Create levelXmlElement element = doc. CreateElement (" Level"); Element. InnerText= status?"0":"1"; Resultsstatus.appendchild (Element); //Create Descriptionelement = Doc. CreateElement ("Description"); Element. InnerText=msg; Resultsstatus.appendchild (Element); //Create passkey If user logon failure will load null characterselement = Doc. CreateElement ("passkey"); Element. InnerText=key; Resultsstatus.appendchild (Element); Results. AppendChild (Resultsstatus); //END Create Resultsstatus//Create DataList data collection if(status) {results. AppendChild (dataList); } //END creates a root node KytresultsDoc. AppendChild (results); stringPath = Server.MapPath ("/ws/xsd/receivereturn.xsd"); //verifying that the XML format is correctly validated by XSD stringError =""; //Statement XmlSchemaXmlSchemaSet schemas =NewXmlSchemaSet (); Schemas. ADD ("", Xmlreader.create (path)); //declaring event handling methodsValidationEventHandler EventHandler =NewValidationEventHandler (Delegate(Objectsender, ValidationEventArgs e) { Switch(e.severity) { CaseXmlSeverityType.Error:error+=E.message; Break; Casexmlseveritytype.warning: Break; } }); Doc. Schemas=schemas; //Validating XMLDoc. Validate (EventHandler); //Check for an exception if the format is incorrect, throw it out. if(!"". Equals (Error)) {Throw NewException (Error); }
Citation Links:
C # uses XSD to validate XML.
C # uses XSD files to verify that the XML is properly formatted
One of the XML validation studies: Validating XML data using XSD schemas
Introduction to C # Two ways of reading XML
A key recommended blog post: xmlspy/xsd and validation
C # uses XSD files to verify that the XML is properly formatted