0
我comapring我的xmlString的XMLSchema如下代碼(我使用架構與任何元素):
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)例外,我是越來越根元素缺失。
爲了避免上述錯誤:當我的根元素添加到我的架構:
(參考: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.
請讓我知道如何解決這個問題。
由於刪除
</xs:element>
:但它是扔錯誤:type屬性不能存在與任一簡單類型或複合。 – channa@channa,我已經更新了答案。 – Satpal
@ Satpal:非常感謝你的工作。 – channa