gpt4 book ai didi

.net - XDocument.Validate 命名空间问题

转载 作者:行者123 更新时间:2023-12-05 01:37:18 27 4
gpt4 key购买 nike

我修改了MSDN示例来反射(reflect)我的问题。

当使用命名空间时,我无法像我期望的那样验证文档,而当验证没有命名空间的文档时,无论它是否有错误,它都会进行验证。

Dim errors As Boolean = False

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

Private Function AddNameSpace(ByVal xDoc As XDocument, ByVal ns As XNamespace) As XDocument
For Each element As XElement In xDoc.Descendants
element.Name = ns + element.Name.LocalName
Next
Return xDoc
End Function

Sub Main()
Dim xsdMarkup As XElement = _
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns="http://somenamespace.com" targetNamespace="http://somenamespace.com">
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("http://somenamespace.com", xsdMarkup.CreateReader)

Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child2>content1</Child2>
</Root>

Dim doc2 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>

Dim ns As XNamespace = "http://somenamespace.com"
doc1 = AddNameSpace(doc1, ns)

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

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

End Sub

输出:

验证 doc1

命名空间“http://somenamespace.com”中的元素“Root”在命名空间“http://somenamespace.com”中具有无效的子元素“Child1”。预期的可能元素列表:'Child1'。

doc1 没有验证

验证 doc2

doc2 验证

最佳答案

如果你想要你的 doc1 你把命名空间放在每个元素上的地方是有效的。对于您当前的模式,一个有效的实例是 Root 在 targetNamespace 中,但 ChildX 元素不在任何命名空间中。

第二个问题是模式验证和命名空间的已知问题,验证解析器会为根元素寻找匹配的模式,如果没有,它会进行松散验证,这样您就不会收到验证错误。使用 XmlReader API,您可以要求在这种情况下发出警告,但我不知道如何使用 Validate 方法来做到这一点。所以你需要这样的代码

Imports System
Imports System.Xml
Imports System.Xml.Linq
Imports System.Xml.Schema

Module Module1

Dim errors As Boolean = False

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

Private Function AddNameSpace(ByVal xDoc As XDocument, ByVal ns As XNamespace) As XDocument
For Each element As XElement In xDoc.Descendants
element.Name = ns + element.Name.LocalName
Next
Return xDoc
End Function

Sub Main()
Dim xsdMarkup As XElement = _
<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns="http://somenamespace.com" targetNamespace="http://somenamespace.com" elementFormDefault="qualified">
<xsd:element name='Root'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='Child1' minOccurs='1' maxOccurs='1'/>
<xsd:element name='Child2' minOccurs='1' maxOccurs='1'/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Dim schemas As XmlSchemaSet = New XmlSchemaSet()
schemas.Add("http://somenamespace.com", xsdMarkup.CreateReader)

Dim doc1 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child2>content1</Child2>
</Root>

Dim doc2 As XDocument = _
<?xml version='1.0'?>
<Root>
<Child1>content1</Child1>
<Child3>content1</Child3>
</Root>

Dim ns As XNamespace = "http://somenamespace.com"
doc1 = AddNameSpace(doc1, ns)

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

Console.WriteLine()
Console.WriteLine("Validating doc2")
Dim xrs As New XmlReaderSettings()
xrs.ValidationType = ValidationType.Schema
xrs.ValidationFlags = xrs.ValidationFlags Or XmlSchemaValidationFlags.ReportValidationWarnings
xrs.Schemas = schemas
AddHandler xrs.ValidationEventHandler, AddressOf XSDErrors
errors = False
Using xr1 As XmlReader = doc2.CreateReader()
Using xr2 As XmlReader = XmlReader.Create(xr1, xrs)
While xr2.Read()

End While
End Using
End Using
Console.WriteLine("doc2 {0}", IIf(errors = True, "did not validate", "validated"))

End Sub

End Module

关于.net - XDocument.Validate 命名空间问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8944749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com