Webservice,验证xml文件的问题

abcyzq 2009-04-14 09:04:25
背景:公布了一个webService,供其他公司传回数据给本公司,并存储到数据库。(通过string xmlFile参数传递到本公司)
xmlFile包含有需要存储到数据库的若干表。
描述:在其他公司执行本公司的webservice的方法时,时常有xml文件中的表字段属性值不对的问题,目前只返回给客户xml文件格式不对的提示。
问题:如何返回具体的xml文件那行那个属性的值不对的错误给调用方?而不是只返回给客户xml格式不对,这样的错误描述太模糊。求详细解决思路。

...全文
231 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
abcyzq 2009-04-20
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 orain 的回复:]
写 Schema 最好弄个 XmlSpy 来,要不然可费劲。
[/Quote]

谢谢你的回答,哈哈,我试验了下,确实如你所说,分都是你的了,是你应得的:)
TkingCN 2009-04-14
  • 打赏
  • 举报
回复
接个分 顶一下
alanfoxye 2009-04-14
  • 打赏
  • 举报
回复
mark
abcyzq 2009-04-14
  • 打赏
  • 举报
回复
谢谢orain兄弟,我试试先。
orain 2009-04-14
  • 打赏
  • 举报
回复
用 XmlReader 你还可以把整个错误节点都打印出来:
	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());
}
}
kbtjh 2009-04-14
  • 打赏
  • 举报
回复
学习一下!!
orain 2009-04-14
  • 打赏
  • 举报
回复
可以啊。
	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: 错误所在行
}
}
yagebu1983 2009-04-14
  • 打赏
  • 举报
回复
验证XML的架构。。。。
orain 2009-04-14
  • 打赏
  • 举报
回复
写 Schema 最好弄个 XmlSpy 来,要不然可费劲。
orain 2009-04-14
  • 打赏
  • 举报
回复
这是 Schema 文档,其实都是 MSDN 上的,呵呵

<?xml version="1.0" encoding="utf-8"?>
<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>
orain 2009-04-14
  • 打赏
  • 举报
回复
这是XML文档

<bookstore xmlns="http://www.contoso.com/books">
<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>
abcyzq 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 orain 的回复:]
Schema 文件得自己写,其实写 Schema 也蛮复杂的,如果没接触过光凭别人说我想是不大能搞懂的,给你个网址,你看看有没有用:
Schema 教程
[/Quote]

兄弟,你好,你看看我6楼的问题,如果能满足我的需要,我就用这个xsd验证,不能我就要想其他的法子。
Teng_s2000 2009-04-14
  • 打赏
  • 举报
回复
还是感觉使用xsd验证一下xml的节点是否有效,比较可靠点!
orain 2009-04-14
  • 打赏
  • 举报
回复
Schema 文件得自己写,其实写 Schema 也蛮复杂的,如果没接触过光凭别人说我想是不大能搞懂的,给你个网址,你看看有没有用:
Schema 教程
abcyzq 2009-04-14
  • 打赏
  • 举报
回复
5楼的兄弟:
你给的代码e.Message会包含xml文件具体那行那个属性值的错误信息么?我之前没用过这样的做法,问下。
orain 2009-04-14
  • 打赏
  • 举报
回复
如果你是用 XmlReader 读取,校验的形式如下:
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 上的样例
烈火蜓蜻 2009-04-14
  • 打赏
  • 举报
回复
你可以创建一个与XML相对应的类,然后把XML反序列化成类,然后判断类的值
itcrazyman 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jie3614 的回复:]
1.可以在先验证xml文件中的表字段属性值是否正确在传回数据给本公司,保证xml格式的正确性
2.定义XSD文件,验证传回的XML文件,将XSD的错误信息返回给客户
[/Quote]
mark up 帮顶
abcyzq 2009-04-14
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 jie3614 的回复:]
1.可以在先验证xml文件中的表字段属性值是否正确在传回数据给本公司,保证xml格式的正确性
2.定义XSD文件,验证传回的XML文件,将XSD的错误信息返回给客户
[/Quote]

你的意思是可以定义xsd文件,通过这个xsd文件可以验证xml文件那个属性那个值出错了?可以自动定位到xml文件出错的行?
可以贴点代码瞧瞧么?
比如我有一个表Emloyee,有记录20条,其中有个属性Name,string类型,长度不大于10,我怎样定义xsd文件,怎样验证(假设第10条记录的Name="1234567890qwe")
jie3614 2009-04-14
  • 打赏
  • 举报
回复
1.可以在先验证xml文件中的表字段属性值是否正确在传回数据给本公司,保证xml格式的正确性
2.定义XSD文件,验证传回的XML文件,将XSD的错误信息返回给客户
加载更多回复(4)

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧