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

解决方案 »

  1.   

    1.可以在先验证xml文件中的表字段属性值是否正确在传回数据给本公司,保证xml格式的正确性
    2.定义XSD文件,验证传回的XML文件,将XSD的错误信息返回给客户
      

  2.   


       你的意思是可以定义xsd文件,通过这个xsd文件可以验证xml文件那个属性那个值出错了?可以自动定位到xml文件出错的行?
       可以贴点代码瞧瞧么?
       比如我有一个表Emloyee,有记录20条,其中有个属性Name,string类型,长度不大于10,我怎样定义xsd文件,怎样验证(假设第10条记录的Name="1234567890qwe")
      

  3.   

    你可以创建一个与XML相对应的类,然后把XML反序列化成类,然后判断类的值
      

  4.   

    如果你是用 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 上的样例
      

  5.   

    5楼的兄弟:
        你给的代码e.Message会包含xml文件具体那行那个属性值的错误信息么?我之前没用过这样的做法,问下。
      

  6.   

    Schema 文件得自己写,其实写 Schema 也蛮复杂的,如果没接触过光凭别人说我想是不大能搞懂的,给你个网址,你看看有没有用:
    Schema 教程
      

  7.   

    还是感觉使用xsd验证一下xml的节点是否有效,比较可靠点!
      

  8.   


    兄弟,你好,你看看我6楼的问题,如果能满足我的需要,我就用这个xsd验证,不能我就要想其他的法子。
      

  9.   

    可以啊。
    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: 错误所在行
    }
    }
      

  10.   

    用 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());
    }
    }
      

  11.   

    orain兄弟,有没可以成功检测xml文件的xsd文件?给一个哈。
      

  12.   

    我手写了几个xml,xsd文件,都有问题,郁闷。
      

  13.   

    我觉得用xsd是最好的解决方法,如果你将来你有其它规则,你修改Schema就可以了,不要怕麻烦!
      

  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>
      

  15.   

    这是 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>
      

  16.   

    写 Schema 最好弄个 XmlSpy 来,要不然可费劲。