简体   繁体   中英

TargetNameSpace usage for xmlSchema Hindesrs XML Validation against XMLSchema


I was Validating My XML against XMLSchema , If i assign any targetnamespace it will throw errors.

my code is as below.


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()) ;

it was throwing error:

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

if I remove TargetNamespace it will work fine if i provide processContents=""lax"" for any element.

please let me know how can make my targetnamespace usage correctly inorder to work(so that i can remove processContents=""lax"" as it takes default "strict" for particularnamespaece.)

Regards,
Channa

Add the xmlns="root" to both xs:schems and xs:element :

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

Could be related to this MS article: BUG: Type "###" is not declared in reference to local type of an included XSD Schema file .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM