Validating Xml Namespace using XmlReaderSettings

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<p align=left>Hello,
<p align=left>Im having some trouble using the XmlReaderSettings in conjunction with an XmlValidatingReader when trying to validate the Xml namespace in an Xml document as part of a unit test.
<p align=left> 
<p align=left>I have an Xml document that looks something like the following (this is just a fragment, but it gets the point across):
<p align=left>  <font color="#0000ff" size=2>
<?</font><font color="#a31515" size=2>xml</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>version</font><font color="#0000ff" size=2>=</font><font size=2>"</font><font color="#0000ff" size=2>1.0</font><font size=2>"</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>encoding</font><font color="#0000ff" size=2>=</font><font size=2>"</font><font color="#0000ff" size=2>utf-8</font><font size=2>"</font><font color="#0000ff" size=2> ?>
<</font><font color="#a31515" size=2>Macros</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>xmlns</font><font color="#0000ff" size=2>=</font><font size=2></font><font color="#0000ff" size=2><a title="http://schemas.modhul.com/BizTalk/Pipeline/Components http://schemas.modhul.com/BizTalk/Pipeline/Components http://schemas.modhul.com/BizTalk/Pipeline/Components </font><font size=2></font><font color="#0000ff" size=2>>

<blockquote dir=ltr style="margin-right:0px
<!--</font><font color="#008000" size=2> Message Tracking Properties </font><font color="#0000ff" size=2>-->
<</font><font color="#a31515" size=2>Macro</font><font color="#0000ff" size=2> </font><font color="#ff0000" size=2>name</font><font color="#0000ff" size=2>=</font><font size=2>"</font><font color="#0000ff" size=2>ActivityIdentity</font><font size=2>"</font><font color="#0000ff" size=2>>
<blockquote dir=ltr style="margin-right:0px
<</font><font color="#a31515" size=2>ContextProperty</font><font color="#0000ff" size=2>></font><font size=2>ActivityIdentity</font><font color="#0000ff" size=2></</font><font color="#a31515" size=2>ContextProperty</font><font color="#0000ff" size=2>>
<</font><font color="#a31515" size=2>ContextPropertyNamespace</font><font color="#0000ff" size=2>></font><font size=2>schemas.microsoft.com</font><font color="#0000ff" size=2></</font><font color="#a31515" size=2>ContextPropertyNamespace</font><font color="#0000ff" size=2>> </blockquote>
</</font><font color="#a31515" size=2>Macro</font><font color="#0000ff" size=2>> </blockquote>
<<font color="#a31515" size=2>Macros<font color="#0000ff ></font></font>
<p align=left><font color="#a31515 </font> 
<p align=left><font color="#a31515 <font color="#000000 However, the correct namespace is:</font> </font><a title="http://schemas.modhul.com/BizTalk/Pipeline/Components/Archive http://schemas.modhul.com/BizTalk/Pipeline/Components/Archive <font color="#000000 http://schemas.modhul.com/BizTalk/Pipeline/Components/Archive </font>
<p align=left> 
<p align=left><font color="#000000 When I validate the Xml document against an Xml Schema (which defines the document detailed above and uses the correct namespace), I do not receive an <font size=2>XmlSchemaValidationException as expected. </font></font>
<p align=left><font color="#000000" size=2></font> 
<p align=left><font color="#000000" size=2>The validation code I am using is as follows (the code compiles fine and throws the exception when the structure is incorrect or I use an incorrect type (i.e. a character string in an int element)):</font>
<p align=left><font color="#2b91af" size=2></font>  <font color="#2b91af" size=2><font size=2>
</font><font color="#2b91af" size=2>XmlReaderSettings</font><font size=2> xmlReaderSettings = </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlReaderSettings</font><font size=2>();
xmlReaderSettings.ValidationType = </font><font color="#2b91af" size=2>ValidationType</font><font size=2>.Schema;
xmlReaderSettings.CheckCharacters = </font><font color="#0000ff" size=2>true</font><font size=2>;
xmlReaderSettings.IgnoreComments = </font><font color="#0000ff" size=2>true</font><font size=2>;
xmlReaderSettings.IgnoreProcessingInstructions = </font><font color="#0000ff" size=2>true</font><font size=2>;
xmlReaderSettings.IgnoreWhitespace = </font><font color="#0000ff" size=2>true</font><font size=2>;
xmlReaderSettings.Schemas.Add(<font color="#a31515" size=2><a title="http://schemas.modhul.com/BizTalk/Pipeline/Components/Archive http://schemas.modhul.com/BizTalk/Pipeline/Components/Archive http://schemas.modhul.com/BizTalk/Pipeline/Components/Archive </font>, </font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlTextReader</font><font size=2>(</font><font color="#2b91af" size=2>Assembly</font><font size=2>.GetExecutingAssembly().GetManifestResourceStream(MACRO_DEFINITION_SCHEMA)));
</font><font color="#0000ff" size=2></font> 
<p align=left><font color="#0000ff" size=2>using</font><font size=2> (</font><font color="#2b91af" size=2>XmlReader</font><font size=2> xmlValidatingReader = </font><font color="#2b91af" size=2>XmlReader</font><font size=2>.Create(</font><font color="#0000ff" size=2>new</font><font size=2> </font><font color="#2b91af" size=2>XmlTextReader</font><font size=2>(_XmlConfigFile), xmlReaderSettings))
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#0000ff" size=2>try </font><font size=2>
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#0000ff" size=2>while</font><font size=2> (xmlValidatingReader.Read()) { } </blockquote>
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#2b91af" size=2>XmlSchemaValidationException</font><font size=2>)
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#008000" size=2>// Configuration file could not be validated against the XSD. </font><font size=2>
</font><font color="#2b91af" size=2>EventLog</font><font size=2>.WriteEntry(EVENT_LOG_SOURCE, </font><font color="#0000ff" size=2>string</font><font size=2>.Format(</font><font color="#a31515" size=2>"The configuration file at {0} failed to validate. Please review the file and correct any defects."</font><font size=2>, _XmlConfigFile), </font><font color="#2b91af" size=2>EventLogEntryType</font><font size=2>.Error, (</font><font color="#0000ff" size=2>int</font><font size=2>)</font><font color="#2b91af" size=2>EventIds</font><font size=2>.</font><font color="#2b91af" size=2>ConfigurationFileValidation</font><font size=2>.XsdValidationError);
</font><font color="#0000ff" size=2>return</font><font size=2> (</font><font color="#0000ff" size=2>false</font><font size=2>); </blockquote>
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (</font><font color="#2b91af" size=2>Exception</font><font size=2>)
{
<blockquote dir=ltr style="margin-right:0px
</font><font color="#008000" size=2>// A general error was encountered while validating the configuration file. </font><font size=2>
</font><font color="#2b91af" size=2>EventLog</font><font size=2>.WriteEntry(EVENT_LOG_SOURCE, </font><font color="#0000ff" size=2>string</font><font size=2>.Format(</font><font color="#a31515" size=2>"The configuration file at {0} failed to validate. Please review the file and correct any defects."</font><font size=2>, _XmlConfigFile), </font><font color="#2b91af" size=2>EventLogEntryType</font><font size=2>.Error, (</font><font color="#0000ff" size=2>int</font><font size=2>)</font><font color="#2b91af" size=2>EventIds</font><font size=2>.</font><font color="#2b91af" size=2>ConfigurationFileValidation</font><font size=2>.GeneralXsdValidationError);
</font><font color="#0000ff" size=2>return</font><font size=2> (</font><font color="#0000ff" size=2>false</font><font size=2>); </blockquote>
} </blockquote>
} </font></font>
<p align=left> 
<p align=left><font color="#000000 Can anyone shed any light on why the incorrect xml namespace is not throwing a validation error, or suggest a way to validate the namespace?</font>
<p align=left><font color="#000000 </font> 
<p align=left><font color="#000000 Cheers, Nick.</font></font>

View the full article
 
Back
Top