Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I set the schema for an XML file?

0 views
Skip to first unread message

David Thielen

unread,
Nov 17, 2009, 1:07:16 PM11/17/09
to
Hi;

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

Martin Honnen

unread,
Nov 17, 2009, 1:26:58 PM11/17/09
to
David Thielen wrote:

> 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/

David Thielen

unread,
Nov 19, 2009, 11:41:29 AM11/19/09
to
On Tue, 17 Nov 2009 19:26:58 +0100, Martin Honnen <maho...@yahoo.de>
wrote:

thank you!!!

0 new messages