0


I am comapring My XMLString to XMLSchema as Below code (i am using Schema with any Element):

XmlReaderSettings settings = new XmlReaderSettings();
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationType = ValidationType.Schema;
settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema;
settings.ValidationFlags |= XmlSchemaValidationFlags.AllowXmlAttributes;
settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings;

settings.Schemas.Add(null, XmlReader.Create(new StringReader(@"<xs:schema  xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
                                                    <xs:element name=""bpElements"">
                                                    <xs:complexType>
                                                    <xs:sequence>
                                                     <xs:any />
                                                    </xs:sequence>
                                                   </xs:complexType>
                                                </xs:element>
                                             </xs:schema>")));

try
{
    // Create the XmlReader object.
    XmlReader xmlrdr = XmlReader.Create(new StringReader("<root>" + ab + "</root>"), settings);
    // Parse the file. 
    while (xmlrdr.Read()) ;
}
catch (XmlSchemaValidationException ex)
{
    Console.WriteLine("The file could not read the value at XML  format is not correct due to" + ex);
}

1)The Exception i was getting Root Element missing.

in order to Avoid above error :When i Add root Element to my Schema:
(ref :Is it possible to define a root element in an XML Document using Schema?)

settings.Schemas.Add(null, XmlReader.Create(new StringReader(@"<xs:schema  xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
                                                            <xs:element name=""root"" type=""RootElementType""/>
                                                            <xs:complexType name=""RootElementType"">
                                                            <xs:sequence>
                                                             <xs:any />
                                                            </xs:sequence>
                                                           </xs:complexType>
                                                        </xs:element>
                                                     </xs:schema>")));

it will throw error:

The 'xs:schema' start tag on line 1 position 2 does not match the end tag of 'xs:element'. Line 8, position 63.

Please let me know how can this be resolved.

Community
  • 1
  • 1
channa
  • 215
  • 1
  • 3
  • 10

1 Answers1

0

Remove </xs:element> from the string

Heres update code

    settings.Schemas.Add(null, XmlReader.Create(
    new StringReader(@"<xs:schema  xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
                        <xs:element name=""root"" type=""RootElementType""/>
                        <xs:complexType name=""RootElementType"">
                        <xs:sequence>
                         <xs:any />
                        </xs:sequence>
                       </xs:complexType>
                 </xs:schema>")));
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • Thanks: But it is throwing error :The type attribute cannot be present with either simpleType or complexType. – channa Aug 06 '13 at 07:00