我将我的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)异常我的Root Element缺失。
为了避免上述错误:当我将根元素添加到我的架构时:
(参考: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>")));
会抛出错误:
The 'xs:schema' start tag on line 1 position 2 does not match the end tag of 'xs:element'. Line 8, position 63.
请告诉我如何解决这个问题。
答案 0 :(得分:0)
从字符串
中删除</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>")));