背景:公布了一个webService,供其他公司传回数据给本公司,并存储到数据库。(通过string xmlFile参数传递到本公司)
xmlFile包含有需要存储到数据库的若干表。
描述:在其他公司执行本公司的webservice的方法时,时常有xml文件中的表字段属性值不对的问题,目前只返回给客户xml文件格式不对的提示。
问题:如何返回具体的xml文件那行那个属性的值不对的错误给调用方?而不是只返回给客户xml格式不对,这样的错误描述太模糊。求详细解决思路。
xmlFile包含有需要存储到数据库的若干表。
描述:在其他公司执行本公司的webservice的方法时,时常有xml文件中的表字段属性值不对的问题,目前只返回给客户xml文件格式不对的提示。
问题:如何返回具体的xml文件那行那个属性的值不对的错误给调用方?而不是只返回给客户xml格式不对,这样的错误描述太模糊。求详细解决思路。
2.定义XSD文件,验证传回的XML文件,将XSD的错误信息返回给客户
你的意思是可以定义xsd文件,通过这个xsd文件可以验证xml文件那个属性那个值出错了?可以自动定位到xml文件出错的行?
可以贴点代码瞧瞧么?
比如我有一个表Emloyee,有记录20条,其中有个属性Name,string类型,长度不大于10,我怎样定义xsd文件,怎样验证(假设第10条记录的Name="1234567890qwe")
public class Sample { public static void Main() { // Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet(); // Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd"); // Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack); // Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings); // Parse the file.
while (reader.Read()); } // Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e) {
Console.WriteLine("Validation Error: {0}", e.Message);
}
}如果是 XmlDocument:
static void Main()
{
try
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add("http://www.contoso.com/books", "contosoBooks.xsd");
settings.ValidationType = ValidationType.Schema; XmlReader reader = XmlReader.Create("contosoBooks.xml", settings);
XmlDocument document = new XmlDocument();
document.Load(reader); ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationEventHandler); // the following call to Validate succeeds.
document.Validate(eventHandler); // add a node so that the document is no longer valid
XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToFollowing("price", "http://www.contoso.com/books");
XmlWriter writer = navigator.InsertAfter();
writer.WriteStartElement("anotherNode", "http://www.contoso.com/books");
writer.WriteEndElement();
writer.Close(); // the document will now fail to successfully validate
document.Validate(eventHandler);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
} static void ValidationEventHandler(object sender, ValidationEventArgs e)
{
switch (e.Severity)
{
case XmlSeverityType.Error:
Console.WriteLine("Error: {0}", e.Message);
break;
case XmlSeverityType.Warning:
Console.WriteLine("Warning {0}", e.Message);
break;
} }
}都是 MSDN 上的样例
你给的代码e.Message会包含xml文件具体那行那个属性值的错误信息么?我之前没用过这样的做法,问下。
Schema 教程
兄弟,你好,你看看我6楼的问题,如果能满足我的需要,我就用这个xsd验证,不能我就要想其他的法子。
static void Main()
{
try
{
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.Schemas.Add("http://www.contoso.com/books", "../../contosoBooks.xsd");
xrs.ValidationType = ValidationType.Schema;
XmlReader xr = XmlReader.Create("../../contosoBooks.xml", xrs);
while (xr.Read()) ;
}
catch (XmlSchemaException ex)
{
// ex.Message: 错误原因
// ex.LineNumber: 错误所在行
}
}
static void Main()
{
XmlReader xr = null;
try
{
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.Schemas.Add("http://www.contoso.com/books", "../../contosoBooks.xsd");
xrs.ValidationType = ValidationType.Schema;
xr = XmlReader.Create("../../contosoBooks.xml", xrs);
while (xr.Read());
}
//catch (XmlSchemaException ex)
catch (XmlSchemaValidationException ex)
{
Console.WriteLine("原因:{0} 行號:{1}", ex.Message, ex.LineNumber);
Console.WriteLine("錯誤節點:{0}", xr.ReadOuterXml());
}
}
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy" publicationdate="1991-02-15">
<!-- 上面一行我把 ISBN 去掉了 -->
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://www.contoso.com/books" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="bookstore">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="book">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string" />
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string" />
<xs:element minOccurs="0" name="first-name" type="xs:string" />
<xs:element minOccurs="0" name="last-name" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="price" type="xs:decimal" />
</xs:sequence>
<xs:attribute name="genre" type="xs:string" use="required" />
<xs:attribute name="publicationdate" type="xs:date" use="required" />
<xs:attribute name="ISBN" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>