469,268 Members | 1,002 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,268 developers. It's quick & easy.

Trying to validate just a subset of an Xml Document


I'm faced with a dilemma here - I have an XML document and for part of it, I
have an XSD schema to validate it - but not for the rest of it.

Can I still validate at least part of the XML document?

I was hoping to use the XmlDocument.Validate(ValidationEventHandler,
XmlNode) method for that - doesn't seem to work...

My XML looks roughly like this (massively simplified):

<Product value="4420" />
<ValidityDate value="2007-03-01" />
<UserId value="garaio" />
<ExternalReference value="9901" />

I basically have an XSD for the <contract>....</contractpart, so I was
hoping to do something like this:

XmlReaderSettings settings = new XmlReaderSettings();
settings.Schemas.Add(null, fullName);
settings.ValidationType = ValidationType.Schema;

XmlDocument doc = new XmlDocument();
doc.Load(<xml file name>);

XmlSchema contractSchema = XmlSchema.Read(new StreamReader(<xsd file
name>), new ValidationEventHandler(<schema validation handler>));


XmlNode contractNode = doc.SelectSingleNode('//contract');

if (contractNode != null)
doc.Validate(<doc validation handler>, contractNode);
The error message I get is:

"Schema information could not be found for the node passed into Validate.
The node may be invalid in its current position. Navigate to the ancestor
that has schema information, then call Validate again."

and I can't quite make out what it's trying to tell me.....

Any ideas??
I can easily validate a sub-document (if I modify the XML to include ONLY
the parts that I can validate) with a validating XML text reader..... so I
guess the XSD should be okay, really. It's just a matter of applying it when
the XML is loaded into an XmlDocument and I want to validate only a subset
of that document.
Any hints would be most welcome !


Mar 13 '07 #1
0 2441

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by user | last post: by
1 post views Thread by Unknown User | last post: by
4 posts views Thread by Mr. x | last post: by
12 posts views Thread by Jim Anderson | last post: by
2 posts views Thread by bbawa1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.