EDN Admin
Well-known member
<p align=left><font face=Arial size=2></font>
Hi,
<blockquote dir=ltr style="margin-right:0px
<p align=left>Im trying to validate an Xml document using an XmlReader. Here is my code:
<font color="#0000ff" size=1><font color="#0000ff" size=1>
private</font></font><font size=1> </font><font color="#0000ff" size=1><font color="#0000ff" size=1>void</font></font><font size=1> Validate(</font><font color="#2b91af" size=1><font color="#2b91af" size=1>TextReader</font></font><font size=1> source, </font><font color="#0000ff" size=1><font color="#0000ff" size=1>string</font></font><font size=1> xsd)
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReaderSettings</font></font><font size=1> settings = </font><font color="#0000ff" size=1><font color="#0000ff" size=1>new</font></font><font size=1> </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReaderSettings</font></font><font size=1>();</font>
<p align=left><font size=1></font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlUrlResolver</font></font><font size=1> resolver = </font><font color="#0000ff" size=1><font color="#0000ff" size=1>new</font></font><font size=1> </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlUrlResolver</font></font><font size=1>();
settings.ProhibitDtd = </font><font color="#0000ff" size=1><font color="#0000ff" size=1>false</font></font><font size=1>;
resolver.Credentials = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>CredentialCache</font></font><font size=1>.DefaultCredentials;
settings.XmlResolver = resolver;
settings.ValidationFlags = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlSchemaValidationFlags</font></font><font size=1>.ProcessSchemaLocation;
settings.ValidationFlags |= </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlSchemaValidationFlags</font></font><font size=1>.ReportValidationWarnings;
settings.Schemas.XmlResolver = resolver;
settings.Schemas.Add(</font><font color="#0000ff" size=1><font color="#0000ff" size=1>null</font></font><font size=1>, xsd);
settings.Schemas.Compile();
//settings.Schemas.Add(</font><font color="#0000ff" size=1><font color="#0000ff" size=1>null</font></font><font size=1>, </font><font color="#a31515" size=1><font color="#a31515" size=1><a title="http://www.w3.org/2001/xml.xsd http://www.w3.org/2001/xml.xsd http://www.w3.org/2001/xml.xsd </font></font><font size=1>);</font>
<p align=left><font size=1>settings.ValidationType = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>ValidationType</font></font><font size=1>.Schema;
settings.ValidationEventHandler += ValidationHandler;
</font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReader</font></font><font size=1> reader = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReader</font></font><font size=1>.Create(source, settings);
</font><font color="#0000ff" size=1><font color="#0000ff" size=1>while</font></font><font size=1> (reader.Read())
{
}
<p align=left></font> </blockquote>
<p align=left>}
<p align=left>
<p align=left>The xsd of the xml document I want to validate contains an xs:import that points to local xsd (the schemaLocation is, as example "./dc.xsd"). This xsd imports serveral other xsd, one of which is the following:
<p align=left>
<p align=left><xs:import namespace="<a title="http://www.w3.org/XML/1998/namespace http://www.w3.org/XML/1998/namespace http://www.w3.org/XML/1998/namespace "
schemaLocation="<a title="http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd
</xs:import>
<p align=left>
<p align=left>When settings.Schemas.Add(null,xsd) is executed, all nested LOCAL xsd are correctly resolved by the XmlUrlResolver I created. If i inspect during debug my XmlReaderSetting instance i can see tha all the required xsd are correctly loaded, EXCEPT <a title="http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd . Then settings.Schemas.Compile() is executed, an exception is raised telling me that the attribute xml:lang is not defined (its defined in the missing xsd).
<p align=left>This exception is not raised, and validaion is performed correctly, if i manually add the namespace (see the commented line of my code), or if i change the scema location in the xsd file to point to a local copy of xml.xsd (schemaLocation="./xml.xsd").
<p align=left>
<p align=left>I would like to know how can I make this work, because i dont want to add manually anything related to this specific scenario (i.e. i would like to reuse the code). In other words, why this remote location is not resolved?
<p align=left>
<p align=left> </blockquote>
View the full article
Hi,
<blockquote dir=ltr style="margin-right:0px
<p align=left>Im trying to validate an Xml document using an XmlReader. Here is my code:
<font color="#0000ff" size=1><font color="#0000ff" size=1>
private</font></font><font size=1> </font><font color="#0000ff" size=1><font color="#0000ff" size=1>void</font></font><font size=1> Validate(</font><font color="#2b91af" size=1><font color="#2b91af" size=1>TextReader</font></font><font size=1> source, </font><font color="#0000ff" size=1><font color="#0000ff" size=1>string</font></font><font size=1> xsd)
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReaderSettings</font></font><font size=1> settings = </font><font color="#0000ff" size=1><font color="#0000ff" size=1>new</font></font><font size=1> </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReaderSettings</font></font><font size=1>();</font>
<p align=left><font size=1></font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlUrlResolver</font></font><font size=1> resolver = </font><font color="#0000ff" size=1><font color="#0000ff" size=1>new</font></font><font size=1> </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlUrlResolver</font></font><font size=1>();
settings.ProhibitDtd = </font><font color="#0000ff" size=1><font color="#0000ff" size=1>false</font></font><font size=1>;
resolver.Credentials = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>CredentialCache</font></font><font size=1>.DefaultCredentials;
settings.XmlResolver = resolver;
settings.ValidationFlags = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlSchemaValidationFlags</font></font><font size=1>.ProcessSchemaLocation;
settings.ValidationFlags |= </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlSchemaValidationFlags</font></font><font size=1>.ReportValidationWarnings;
settings.Schemas.XmlResolver = resolver;
settings.Schemas.Add(</font><font color="#0000ff" size=1><font color="#0000ff" size=1>null</font></font><font size=1>, xsd);
settings.Schemas.Compile();
//settings.Schemas.Add(</font><font color="#0000ff" size=1><font color="#0000ff" size=1>null</font></font><font size=1>, </font><font color="#a31515" size=1><font color="#a31515" size=1><a title="http://www.w3.org/2001/xml.xsd http://www.w3.org/2001/xml.xsd http://www.w3.org/2001/xml.xsd </font></font><font size=1>);</font>
<p align=left><font size=1>settings.ValidationType = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>ValidationType</font></font><font size=1>.Schema;
settings.ValidationEventHandler += ValidationHandler;
</font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReader</font></font><font size=1> reader = </font><font color="#2b91af" size=1><font color="#2b91af" size=1>XmlReader</font></font><font size=1>.Create(source, settings);
</font><font color="#0000ff" size=1><font color="#0000ff" size=1>while</font></font><font size=1> (reader.Read())
{
}
<p align=left></font> </blockquote>
<p align=left>}
<p align=left>
<p align=left>The xsd of the xml document I want to validate contains an xs:import that points to local xsd (the schemaLocation is, as example "./dc.xsd"). This xsd imports serveral other xsd, one of which is the following:
<p align=left>
<p align=left><xs:import namespace="<a title="http://www.w3.org/XML/1998/namespace http://www.w3.org/XML/1998/namespace http://www.w3.org/XML/1998/namespace "
schemaLocation="<a title="http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd
</xs:import>
<p align=left>
<p align=left>When settings.Schemas.Add(null,xsd) is executed, all nested LOCAL xsd are correctly resolved by the XmlUrlResolver I created. If i inspect during debug my XmlReaderSetting instance i can see tha all the required xsd are correctly loaded, EXCEPT <a title="http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd http://www.w3.org/2001/03/xml.xsd . Then settings.Schemas.Compile() is executed, an exception is raised telling me that the attribute xml:lang is not defined (its defined in the missing xsd).
<p align=left>This exception is not raised, and validaion is performed correctly, if i manually add the namespace (see the commented line of my code), or if i change the scema location in the xsd file to point to a local copy of xml.xsd (schemaLocation="./xml.xsd").
<p align=left>
<p align=left>I would like to know how can I make this work, because i dont want to add manually anything related to this specific scenario (i.e. i would like to reuse the code). In other words, why this remote location is not resolved?
<p align=left>
<p align=left> </blockquote>
View the full article