all groups > dotnet xml > may 2004 >
You're in the

dotnet xml

group:

Validating and XML file with a schema


Validating and XML file with a schema Robert Reineri
5/28/2004 12:55:00 PM
dotnet xml:
Hello,

New to the XML world and .NET. I have what I believe to be a simple problem,
but I have read the .NET docs till I'm blue in the face and still can't
locate a simple example of how to accomplish this.

I have two strings (C# string type).
One of them contains an XML document.
The other one contains a schema.

I want to validate the XML document using the schema.

Can someone suggest a way to do this? The only thing I can gather from the
docs is that I need to use an XmlValidatingReader, and provide an event
handler to respond when something goes wrong, but the validating reader
needs a text reader, which needs a string reader and so on. I was hoping for
something like:

try
{
XmlDocument doc = new XmlDocument();
doc.Load(string xml, string schema);
}

catch
{
Debug.WriteLine("The validation failed.");
}


Any help would be greatly appreciated.

Thanks

Robert

P.S. Please respond via newsgroup if possible. Thanks.

Re: Validating and XML file with a schema Derek Harmon
5/29/2004 8:10:32 PM
[quoted text, click to view]

The likely reason why you're having difficulty finding such an example
is because holding XML in a String is very inefficient. Your XML
probably comes from some other source that can be represented
in a Stream or Reader, why not use that?

: :
[quoted text, click to view]

Looks like you're already aware of the solution.

[quoted text, click to view]

That would encourage inefficient programming practices. ;-)

Where there is support in the .NET Framework for parsing XML from
a UTF-16 String representation, the methods are conventionally named
with an -Xml ending (for example, ReadXml( ) or LoadXml( )). Here
the undecorated method name (Read( ) or Load( )) always operates
on Streams, Readers, or filename/URLs in a String.

Therefore, Load( string) always means a filename/URL, whereas
LoadXml( string) means the String contains UTF-16 encoded XML.

If you need a convenience method just to wrap the few lines of code
necessarily to extract the XML from the String, you can try this,

- - - ValidatingXmlLoader.cs
using System;
using System.IO;
using System.Xml;

public sealed class ValidatingXmlLoader
{
public static XmlDocument Load( string strXml, string strSchemaXml)
{
// Prepare the XmlSchema, Read() throws an XmlException for any XML
// errors present in the .XSD
XmlSchema schema = new XmlSchema( );

// No ValidationEventHandler necessary if you just want XmlException thrown
// on an XML error in the instance document, that's the default behavior.
schema.Read( new StringReader( strSchemaXml), null);

// Create the XmlValidatingReader for the instance document.
XmlValidatingReader reader = new XmlValidatingReader(
new XmlTextReader( new StringReader( strXml)));

// Prepare a DOM document to contain the validated instance document.
XmlDocument doc = new XmlDocument( );

// Load the DOM document; if the strXml is not valid an XmlException will
// be thrown and no document will be returned.
doc.Load( reader);

return doc;
}
}
- - -

In your desired example code, you'd use this sample class like this,

try
{
XmlDocument doc = ValidatingXmlLoader.Load( xml, schema);
}
catch
{
Debug.WriteLine( "The validation failed.");
}



Derek Harmon

Re: Validating and XML file with a schema Derek Harmon
5/29/2004 8:26:51 PM
[quoted text, click to view]
: :
// Attach XmlSchema to XmlValidatingReader.
reader.Schemas.Add( schema);
: :
[quoted text, click to view]

Derek Harmon

Re: Validating and XML file with a schema Robert Reineri
6/1/2004 9:29:30 AM
Derek,

Thank you for the pointer. The XML is actually in a string because I get it
back from a stored procedure call to an Oracle database. The schema is in a
string because I thought it would be cool to store the schema as an embedded
resource in the DLL that implements all this.

I like the small wrapper class, and believe I will go with that idea. Thanks
again.

Robert

[quoted text, click to view]

AddThis Social Bookmark Button