By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,816 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,816 IT Pros & Developers. It's quick & easy.

Trying to validate just a subset of an Xml Document

P: n/a
Folks,

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):

<root>
<header>
....
</header>
<new>
<contract>
<Product value="4420" />
<ValidityDate value="2007-03-01" />
<UserId value="garaio" />
<ExternalReference value="9901" />
....
<details>
.....
......
....
</details>
</contract>
</new>
</root>

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>));

doc.Schemas.Add(contractSchema);

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 !

Marc

Mar 13 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.