我正在针对XMLSchema验证我的XML,如果我指定任何targetnamespace,它将抛出错误。
我的代码如下。
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()) ;
投掷错误:
ex = {"Type 'RootElementType' is not declared."}
如果我删除TargetNamespace,如果我为任何元素提供processContents =“”lax“”,它将正常工作。
请让我知道如何正确使用我的targetnamespace使用(这样我就可以删除processContents =“”lax“”,因为对于specialnamespaece,默认为“strict”。)
的问候,
鳢
答案 0 :(得分:0)
将xmlns="root"
添加到xs:schems
和xs:element
:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="root"
xmlns="root">
<xs:element name="root" type="RootElementType"
xmlns="root"/>
可能与此MS文章有关:BUG: Type "###" is not declared in reference to local type of an included XSD Schema file。