XmlValidatingReader
XmlSchemaCollection
ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
code in quickstart from asp.net
<%@ Webservice Language="C#" Class="MessageValidationService" %>
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.IO;
[WebService(Namespace="Microsoft.Samples.XmlMessaging.WebServices")]
public class MessageValidationService {
private string returnMessage = "Success! Validation was successful.";
[WebMethod]
public string SendToValidator(string input)
{
XmlTextReader tr = new XmlTextReader(input, XmlNodeType.Document, null);
XmlValidatingReader vr = new XmlValidatingReader(tr);
XmlSchemaCollection schemas = new XmlSchemaCollection();
schemas.Add("Microsoft.Samples.Web.Services", "http://66.129.71.130/quickstartv20/ webservices/samples/ MessageValidation/Book.xsd");
vr.Schemas.Add(schemas);
vr.ValidationType = ValidationType.Schema;
vr.ValidationEventHandler += new ValidationEventHandler (ValidationHandler);
try
{
while (vr.Read())
{
//do nothing
}
}
catch (Exception exception)
{
returnMessage = "Failure. An Exception was received, most likely indicating malformed XML. Message: " + exception.Message;
}
return returnMessage;
}
public void ValidationHandler(object sender, ValidationEventArgs args)
{
returnMessage = "Failure. Validation was not successful. Message: " + args.Message;
}
}
参考:Validate Messages QuickStart in Asp.net
没有评论:
发表评论