The XML Schema is validated using XmlValidatingReader in ASP.
During this time, I was learning XML, which describes the schema or DTD that uses xerces-c or xerces-j to validate XML documents, and finds that the tools above are not working well.
Later, only to put down the book, go to the bookstore to see, there is no other books can bring help. When checked to Professiona ASP. NET XML with C # This book, it said that the use of System.Xml and System.Xml.Schema the XmlTextReader class and the XmlValidatingReader class under the namespace to validate an XML document with a schema or DTD. I bought it.
Back to the bedroom, according to the book in the introduction, wrote a small test procedures, found that it is also very useful, here, but posted out, hoping to give the people who need help.
The code is as follows:
<% @Page debug= "true"%>
<% @Import namespace= "System"%>
<% @Import namespace= "System.Xml"%>
<% @Import namespace= "System.Xml.Schema"%>
<script language= "C #" runat= "Server" >
protected void Uploadbtn_click (Object Sender,eventargs e)
{
XmlTextReader myxml = null;
XmlValidatingReader myxmldoc = null;
int nelements = 0; Number of element in the statistics document
int nattributes = 0; Number of attributes in a statistical document
int ncomments = 0; Number of comment in a statistical document
int NPIs = 0; Number of processinginstruction in a statistical document
int ncdatas = 0; Number of CDATA In a statistical document
String sworkingtext = "";
Resultarea.text = "";
Try
{
Loading XML Document
myXML = new XmlTextReader (FileSelector.PostedFile.InputStream);
myxml.whitespacehandling = WhitespaceHandling.None;
XmlValidatingReader is the help function of XmlTextReader
You need to get an instance from a XmlTextReader object
Myxmldoc = new XmlValidatingReader (myXML);
Myxmldoc.validationtype = ValidationType.Auto;
Verifying the signature of an event delegate
Myxmldoc.validationeventhandler + = new ValidationEventHandler (this. Validationeventcallback);
When traversing an XML document, the Xmlvalidatingreadert will be based on the schema or DTD
Validates the XML.
while (Myxmldoc.read ())
{
The main function of this statement is to parse each node of the XML document,
But the while loop is necessary because only the node that iterates through all of them will check to see if the validation matches.
Switch (myxmldoc.nodetype)//judge crrent Node ' s type. Determine the type of the current node
{
Case XmlNodeType.Element://element elements
++nelements;
Nattributes + = Myxmldoc.attributecount;
Break
Case XmlNodeType.Text://text content
Sworkingtext = "<b>text:</b>" + myxmldoc.value + "<br/>";
Resultarea.text + = Sworkingtext;
Break
Case Xmlnodetype.comment://Comment
++ncommen;
Break
Case Xmlnodetype.processinginstruction://processing instructions
++npis;
Break
Case Xmlnodetype.cdata://cdata
++ncdatas;
Break
}
}
sworkingtext = "<br/>" + "total number of Elements: "+ nelements +" <br/> ";
Sworkingtext + =" Total number of attributes in document: "+ Nattributes + "<br/>";
Sworkingtext + = "Total Comments:" + ncomments + "<br/>";
Sworkingtext + = "All P Rocessinginstructions: "+ NPIs +" <br/> ";
Sworkingtext + =" Total CDATA sections: "+ ncdatas;
Resultarea.text + = sworkingtext;
}
catch (XmlException exec)
{
Sworkingtext = " Exception while parsing: "+" <br/> ";
Sworkingtext + =" line number: "+ exec. LineNumber + "<br/>";
Sworkingtext + = "Message:" + exec. Message + "<br/>" + "<br/>";
Sworkingtext + = "line position:" + exec. Lineposition + "<br/>";
Sworkingtext + = "Stack Trace:" + "<br/>" + exec. StackTrace;
Resultarea.text = Sworkingtext;
}
Finally
{
if (myxmldoc!=null)
{
Myxmldoc.close ();
}
}
}