I'm sure this is straightforward but I can't find it. How do I pass a
schema file with an XML file when I load the xml file? I need this
mostly for an XPathNavigator object I create.
??? - thanks - dave
david@at-at-at@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com
Cubicle Wars - http://www.windwardreports.com/film.htm
> I'm sure this is straightforward but I can't find it. How do I pass a
> schema file with an XML file when I load the xml file? I need this
> mostly for an XPathNavigator object I create.
Create XmlReaderSettings that has any schemas and validation option,
then load the XML with an XmlReader created with those settings e.g.
XmlReaderSettings xrs = new XmlReaderSettings();
xrs.ValidationType = ValidationType.Schema;
xrs.Schemas.Add(null, "schema1.xsd");
// add further schemas as needed e.g.
//xrs.Schemas.Add(null, "schema2.xsd");
// add ValidationEventHandler if you don't want an exception
// in the case of validation problems e.g.
//xrs.ValidationEventHandler += new
ValidationEventHandler(xrs_ValidationEventHandler);
XmlDocument doc = new XmlDocument();
using (XmlReader xr = XmlReader.Create("input.xml", xrs))
{
doc.Load(xr);
}
You could also pass such an XmlReader to the XPathDocument constructor,
in case you work with XPathDocument and not XmlDocument.
If you already have an XmlDocument and want to add type annotations then
use the Validate method
http://msdn.microsoft.com/en-us/library/ms162371.aspx
See also http://msdn.microsoft.com/en-us/library/w5aahf2a.aspx
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/