Since we have learned the AddCategories method, let's take a look at the assistant methods used by the AddCategories method. First, let's take a look at the validatexml method. As mentioned earlier, the method is responsible for ensuring that the Categories XML data being added is complied with the pre-defined XML schema in categories.xsd.
Private bool validateXML (String XML)
{
Bool validxml = false;
// load the xml data inTo memory
XMLValidatingReader Valreader = New
XMLValidatingReader (XML, XMLNodeType.Document, NULL);
ValReader.schemas.Add (Null, Server.mappath ("Categories.xsd"));
Valreader.validationType = validationtype.schema;
ValReader.validationEventHndler = New
ValidationEventHandler (ValidationHandler);
// loop through the xml file
While (ValReader.Read ())
{}
IF (Builder.Length> 0)
Validxml = false;
Else
Validxml = true;
ValReader.close ();
Return Validxml;
}
The above code first passes the added XML data to the constructor, creates an XMLValidatingReader class instance. Then, add categories.xsd to the Schemas collection of the XMLValidatingReader object. Then, set validationType to ValidationType.Schema, indicating that we are verifying XML data based on XML mode. When you verify XML data in using the XMLValidatingReader class, you must create an event handleable object and associate it with the ValidationEventHandler event. Once finished, the check error and warning are reported through this callback event. ValidationEventHandler has a parameter for a validationEventArgs type. The ValidationEventArgs class offers two important properties, Message and Serverity. These two attributes provide more information about calibration errors.
In this case, we associate the validationEventHandler event with the ValidationHandler method. In this method, we add an error message to the StringBuilder object, and the STRINGBUILDER object is defined in the module. If there is no verification error, the LENGTH attribute of the StringBuilder object will return 0. We are using this to check if the XML mode verification failed. The ValidationHandler method is defined below.
Public void validationHandler (Object Sender,
ValidationEventArgs args
{
Builder.Append ("Validation Error" "
);
Builder.Append ("Severity:" args.severity "
);
Builder.Append ("Message: args.Message "
);}
Below, let's take a look at the code of the RaiseException method.
Public SOAPEXCEPTION RAISEEXCEPTION (STRING URI,
String WebServicenamespace,
String ErrorMessage,
String ErrorNumber,
String Errorsource,
Faultcode code)
{
XmlqualifiedName Faultcodelocation = NULL;
// identify the location of the faultcode
Switch (Code)
{
Case Faultcode.Client:
Faultcodelocation = soapexception.clientfaultcode;
Break;
Case Faultcode.server:
Faultcodelocation = sopexception.serverfaultcode;
Break;
}
XMLDocument Xmldoc = new xmldocument ();
// Create the detail node
Xmlnode rootnode = xmldoc.createnode (XMLNodettype.element,
SOAPEXCEPTION.DETAILEMENTNAME.NAME,
SOAPEXCEPTION.DETAILEMENTNAME.NAMESPACE);
// build Specific Details for the soapexception
// Add first child of detail XML ELEMENT.
XMLNode ErrorNode = Xmldoc.createnode (XMLNodetype.element, "Error",
WebServicenamespace;
// Create and set the value for the errornumber node
XMLNode ErrorNumberNode =
XMLDoc.createnode (XMLNodettype.element, "ErrorNumber",
WebServicenamespace;
ErrorNumbernode.innertext = ErrorNumber;
// create and set the value for the errorMessage Node
XMLNode ErrorMessageNode = Xmldoc.createnode (XMLNodettype.element,
"ErrorMessage",
WebServicenamespace;
ErrorMessagenode.innertext = ErrorMessage;
// Create and set the value for the Errorsource Node
XMLNode ErrorsourceNode =
Xmldoc.createnode (XMLNodettype.element, "Errorsource",
WebServicenamespace;
ErrorsourceNore.innertext = Errorsource;
// append the error child element nodes to the root detail node.
ErrorNode.Appendchild (ErrorNumbernode);
ErrorNode.Appendchild (ErrorMessageNode); ErrorNode.Appendchild (ErrorSourCenode);
// append the detail node to the root node
Rootnode.Appendchild (ERRORNODE);
// Construct the Exception
SOAPEXCEPTION SOAPEX = New SOAPEXCEPTION (ERRORMESSAGE,
FaultcoDelocation, URI,
Rootnode);
// raise the Exception Back to the Caller
Return SOAPEX;
}
As suggested, the RaiseException method is used to throw an exception in the web service in the form of a SOAPEXCEPTION object. The code shown above first checks the value of the enumeration parameters contained in the faultcode, and the enumeration parameter is used to identify the source of the abnormality. If it is an exception due to the problem (for example, the database server has been turned off), the value of FaultCode should be set to SOAPEXCEPTION.SERVERFAULTCODE. Next, the RaiseException method creates an XMLDocument object to save the content of the Detail element. This object adds all child elements under the Detail element and then passes the Detail node to the constructor of the SOAPEXCEPTION object. Finally, the method returns the SOAPEXCEPTION object to the caller with the RETURN statement. If you check the Detail element inside the SOAPEXCEPTION object, you will find it a bit similar to the following.
Error>
detail>
When the client application receives an exception to the web service, it can view the Detail property of the SOAPEXCEPTION object to get more information about the generated exception.
Use the advantages of SOAPEXCEPTION:
Use the SOAPEXCEPTION class to return the exception information to the web service client, which has many advantages. as follows:
l The abnormal situation can be handled in a consistent manner.
l Based on SOAP specifications
l By displaying an abnormality, this can communicate more information, such as an exception cause, URL of the web service method, etc. (using properties such as actors, code, and detail)
l Use FaultCode to clearly indicate an abnormality attributed to the client or server
l Use the Detail property to describe exception information more detail.