访问XMLSchema中的根元素

本文关键字:元素 XMLSchema 访问 | 更新日期: 2023-09-27 18:26:30


我将我的XMLString转换为XMLSchema,如下代码所示(我将Schema与任何元素一起使用):

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) 异常i获取的根元素丢失

为了避免上述错误:当我将根元素添加到我的架构时:
(参考:是否可以使用Schema在XML文档中定义根元素?)

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

它将抛出错误:

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

请告诉我如何解决这个问题。

访问XMLSchema中的根元素

从字符串中删除</xs:element>

这是更新代码

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