.net 架构验证

发布于 2024-10-12 22:03:25 字数 1950 浏览 3 评论 0原文

我在根据 svcutil 生成的架构验证 xml 文件时遇到问题。出于此问题的目的,请参阅下面的一段代码,其中包含一个简化的 XSD 架构和我尝试验证的 XML 文档:

Imports System.Xml.Schema

Module Main

    Dim errors As Boolean = False

    Sub Main()

        Try

            Dim xsdMarkup As XElement = _
              <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:tns="http://zen/myservices" targetNamespace="http://zen/myservices">

                  <xs:element name="Car" type="tns:CarType"/>

                  <xs:complexType name="CarType">
                      <xs:sequence>
                          <xs:element name="Make" minOccurs="1" maxOccurs="1"/>
                          <xs:element name="Model" minOccurs="1" maxOccurs="1"/>
                      </xs:sequence>
                  </xs:complexType>

              </xs:schema>
            Dim schemas As XmlSchemaSet = New XmlSchemaSet()
            schemas.Add("http://zen/myservices", xsdMarkup.CreateReader)

            Dim doc1 As XDocument = _
             <?xml version='1.0'?>
             <Car>
                 <Makee>content1</Makee>
                 <Model>content1</Model>
             </Car>


            Console.WriteLine("Validating doc1")
            errors = False
            doc1.Validate(schemas, AddressOf XSDErrors)
            Console.WriteLine("doc1 {0}", IIf(errors = True, "did not validate", "validated"))


        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

        Console.WriteLine("Hit <ENTER> to exit...")
        Console.ReadKey()

    End Sub

    Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
        Console.WriteLine("{0}", e.Message)
        errors = True
    End Sub

End Module

在这种特殊情况下的验证应该失败(“Make ' 元素拼写错误)。有趣的是,尽管它过去了。

我在这段代码中缺少什么想法吗?

感谢您的帮助。

I am experiencing a problem with validating an xml file against a schema which was generated by svcutil. For the purpose of this question please see below a snippet of code which contains only a simplified XSD schema and the XML document that I am trying to validate:

Imports System.Xml.Schema

Module Main

    Dim errors As Boolean = False

    Sub Main()

        Try

            Dim xsdMarkup As XElement = _
              <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:tns="http://zen/myservices" targetNamespace="http://zen/myservices">

                  <xs:element name="Car" type="tns:CarType"/>

                  <xs:complexType name="CarType">
                      <xs:sequence>
                          <xs:element name="Make" minOccurs="1" maxOccurs="1"/>
                          <xs:element name="Model" minOccurs="1" maxOccurs="1"/>
                      </xs:sequence>
                  </xs:complexType>

              </xs:schema>
            Dim schemas As XmlSchemaSet = New XmlSchemaSet()
            schemas.Add("http://zen/myservices", xsdMarkup.CreateReader)

            Dim doc1 As XDocument = _
             <?xml version='1.0'?>
             <Car>
                 <Makee>content1</Makee>
                 <Model>content1</Model>
             </Car>


            Console.WriteLine("Validating doc1")
            errors = False
            doc1.Validate(schemas, AddressOf XSDErrors)
            Console.WriteLine("doc1 {0}", IIf(errors = True, "did not validate", "validated"))


        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try

        Console.WriteLine("Hit <ENTER> to exit...")
        Console.ReadKey()

    End Sub

    Private Sub XSDErrors(ByVal o As Object, ByVal e As ValidationEventArgs)
        Console.WriteLine("{0}", e.Message)
        errors = True
    End Sub

End Module

The validation in this particular case should fail (the 'Make' element has been misspelled). Interestingly enough though it passes.

Any ideas what am I missing in this code?

Your help is appreciated.

Zen

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫南 2024-10-19 22:03:25

一切就绪。我自己解决了这个问题。我不小心遗漏了正在验证的 XML 中的名称空间定义:

<?xml version='1.0'?>          
<Car>              
    <Makee>content1</Makee>              
    <Model>content1</Model>          
</Car> 

应该是:

<?xml version='1.0'?>
<tns:Car xmlns:tns="http://zen/myservices">
    <Makee>content1</Makee>
    <Model>content1</Model>
</tns:Car>

验证现在按预期失败。

All set. I resolved the issue myself. I had accidentally left out the namespaces definition in the XML being validated:

<?xml version='1.0'?>          
<Car>              
    <Makee>content1</Makee>              
    <Model>content1</Model>          
</Car> 

should have been:

<?xml version='1.0'?>
<tns:Car xmlns:tns="http://zen/myservices">
    <Makee>content1</Makee>
    <Model>content1</Model>
</tns:Car>

Validation is failing now as expected.

Zen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文