0


我正在针对 XMLSchema 验证我的 XML,如果我分配任何目标命名空间,它将引发错误。

我的代码如下。


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=""lax"" 它将正常工作。

请让我知道如何使我的目标命名空间正确使用才能正常工作(以便我可以删除 processContents=""lax"" 因为它对特定命名空间采用默认的“严格”。)

问候,
香娜

4

1 回答 1

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

于 2013-08-07T07:16:46.130 回答