Skip to main content
gamedev.net
🔒 Locked

XML Schema Validation

Started by Puzzler183 Apr 27, 2004 at 4:30 PM 6 replies 750+ views
Original Post
Puzzler183
Puzzler183
For a long time, I''ve known how to creat XML Schema, but I''ve never found a use before the metaprogramming project I''m working on now. So I made my XSD and a test XML file and tried to use browsers to validate. No browser would give any errors. So I opened up VS .NET, and try validating there. It refuses to find my schema... To make sure it''s working, I have it make a schema for my document, and it still lets anything through, saying it can''t find the schema. So I''m wondering, has anyone found a good way to validate XML based on XSD?
fredizzimo
fredizzimo
W3C have some links to tools for it, and links to a lot of other resources too.
Puzzler183
Puzzler183
I know, but none of the tools on that list that I liked were free. I''m basically shooting for recommendations/way to get VS .NET to work because Google can find me plenty of junk. Thanks though.
Puzzler183
Puzzler183
Actually, there are no namespaces. I''ve specified a no namespace schema and so I think I''ll post relevant code when I get home because this would be easiest if VS .NET would just do it.
Arild Fines
Arild Fines
Uh, how can you have a schema without a namespace? I didn''t think that was possible.
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
Calefaction
Calefaction
To validate against a schema, you need to do two things (these are library specific things, but I am going to show the .NET code I use myself to do XML schema validation, your mileage may vary).

First, the schema itself must have a default xmlns equivalent to the XML schema namespace, for instance:
xmlns="http://dnpb.sourceforge.net/schemas/dnpb-1.2.xsd"

In the XML file you want to be validated by this schema, also do:
xmlns="http://dnpb.sourceforge.net/schemas/dnpb-1.2.xsd"

Then you need to code to add the actual schema to the validating reader, something like:


private static string m_SchemaURI = "http://dnpb.sourceforge.net/schemas/dnpb-1.2.xsd";
private XmlSchemaCollection m_Schemas = null;

private void LoadSchema()
{
Assembly assembly = this.GetType().Assembly;
Stream stream = assembly.GetManifestResourceStream("DNPreBuild.data." + m_Schema);
XmlReader schema = new XmlTextReader(stream);

m_Schemas = new XmlSchemaCollection();
m_Schemas.Add(m_SchemaURI, schema);
}

private void ProcessFile(string file)
{
string path = file;
try
{
XmlTextReader reader = new XmlTextReader(path);
XmlValidatingReader valReader = new XmlValidatingReader(reader);
valReader.Schemas.Add(m_Schemas);

XmlDocument doc = new XmlDocument();
doc.Load(valReader);
}
catch(XmlSchemaException xse)
{
m_Log.Write("XML validation error at line {0} in {1}:\n\n{2}",
xse.LineNumber, path, xse.Message);
}
}

public void Initialize()
{
LoadSchema();
}


The mapping between schema and document is done via the namespace of the two. Meaning that all elements in namespace Foo will be validated against the schema with the namespace Foo, if one is loaded in the schemas collection.

Also note that you CAN specify a specific namespace if you don't want to validate inside the default namespace, for instance:

xmlns:dnpb="http://dnpb.sourceforge.net/schemas/dnpb-1.2.xsd"

This would allow you to validate a document against several schemas at once, provided you properly qualified your tags (I only use one schema in my project, so the use of an extra namespace is worthless to me).

Hope this helps

[edited by - Calefaction on April 28, 2004 8:48:51 PM]

[edited by - Calefaction on April 28, 2004 8:49:53 PM]

[edited by - Calefaction on April 28, 2004 8:51:58 PM]
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
Puzzler183
Puzzler183
Well the thing is, it won''t even validate my document when I have it generate a schema, and that''s really bad IMO. So anyway, I''m just going to go without schemata as they are basically worthless with this much extra junk.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.