2013-08-07 23 views
0


的targetNamespace使用率XMLSCHEMA Hindesrs XML驗證對XMLSchema的

我驗證我的XML對XMLSchema的,如果我指定的任何目標名稱會引發錯誤。

我的代碼如下。


string 
ab="<HostName>Arasanalu</HostName><AdminUserName>Administrator</AdminUserName> 
    <AdminPassword>A1234</AdminPassword><PlaceNumber>38</PlaceNumber>" 


     try 
     { 
     XmlReaderSettings settings = new XmlReaderSettings(); 

     settings.ValidationType = ValidationType.Schema; 
     settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; 
     settings.ValidationFlags |= XmlSchemaValidationFlags.AllowXmlAttributes; 
     settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; 
     // settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); 


     //settings.Schemas.Add("http://www.w3.org/2001/XMLSchema","ab1.xml"); 

     settings.Schemas.Add(null, XmlReader.Create(new StringReader(@"<xs:schema xmlns:xs=""http://www.w3.org/2001/XMLSchema"" targetNamespace=""root""> 
                  <xs:element name=""root"" type=""RootElementType""/> 
                  <xs:complexType name=""RootElementType""> 
                   <xs:sequence> 
                  <xs:any minOccurs=""1"" maxOccurs=""unbounded"" processContents=""lax""/> 
                  </xs:sequence> 
                  </xs:complexType> 
                 </xs:schema> 
                  <bp:root xmlns:bp=""myNamespace""> 
                  <parameters>ab</parameters> 
                  </bp:root> 
                  </root>"))); 


      // Create the XmlReader object. 
       XmlReader xmlrdr = XmlReader.Create(new StringReader("<root>" + ab+ "</root>"),settings); 

       // Parse the file. 
       while (xmlrdr.Read()) ; 

它被扔的錯誤:

ex = {"Type 'RootElementType' is not declared."} 

如果我刪除的targetNamespace它會正常工作,如果我提供的processContents = 「」 鬆懈 「」 任何元素。

請讓我知道如何才能正確地使我的目標名稱使用序工作(這樣我可以刪除的processContents =「」鬆懈「」因爲它需要默認的「嚴格」的particularnamespaece。)

問候,

+0

'ab'的內容是什麼? –

+0

@ Alex:我已經爲字符串ab添加了信息,那是我的xml字符串。 – channa

回答

0

添加xmlns="root"xs:schemsxs:element

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
      targetNamespace="root" 
      xmlns="root"> 
    <xs:element name="root" type="RootElementType" 
       xmlns="root"/> 

可能與此MS文章:BUG: Type "###" is not declared in reference to local type of an included XSD Schema file

+0

謝謝但它沒有按我的要求工作。如果我發送我的字符串ab的任何inavlid作爲 ABC 它應該拋出XmlSchemaValidationException但直接傳遞。 – channa