2012-01-20 138 views
3

我修改了MSDN示例以反映我的問題。XDocument.Validate命名空間問題

當使用一個命名空間,我不能獲取文檔驗證,因爲我所期望的和驗證的文檔時,這並不具有不論它是否具有或不出錯它驗證一個名稱空間。

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具有無效子元素 'Child1' 在命名空間的「http:// somenamespace .COM」。預計可能的元素列表:'Child1'。

DOC1沒有驗證

驗證DOC2

DOC2驗證

回答

5

那麼你將需要添加elementFormDefault="qualified"到您的模式(在xsd:schema元素),如果你希望你的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