Groups | Blog | Home
all groups > dotnet xml > october 2007 >

dotnet xml : How to link xml with xsd "externally"?


Eve
10/18/2007 6:33:00 AM
Right now, in order to validate my xml file against xsd, I include the
following line in the xml file: xmlns="urn:mySchemaName".
Is it possible to link xml to xsd without specifying the schema inside the
xml file?
Eve
10/18/2007 7:12:00 AM
Yes, I am using XmlReaderSettings (I realize xmlns="urn:mySchemaName" is just
a namespace, not an instruction). I was wondering if it's possible to not
specify a namespace in the xml doc. The reason I want to do that is I don't
know what xsd I will be using up in front and I want to set it on the fly
programmatically(withouth touching the original xml doc).

[quoted text, click to view]
Eve
10/18/2007 8:31:01 AM
Martin,
I am using null as the targetNamespace (please look at the code below), but
I still need to include xmlns="mySchemaHere" in the xml that I'm validating.
If I don't do that, I get messages saying something like "schema not defined
for X element".
Is it possible to omit the xmlns in my xml doc and still somehow
programatically link the doc to the xsd?

public class XMLValidator
{
// Validation Error Count
static int ErrorsCount = 0;

// Construct a validation error message
static string ErrorMessage = "";

private static void ValidationHandler(object sender,
ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Error || e.Severity ==
XmlSeverityType.Warning)
{
ErrorsCount++;
ErrorMessage += String.Format(ErrorsCount + ". Line {0},
Position {1} \"{2}\"",
e.Exception.LineNumber, e.Exception.LinePosition,
e.Exception.Message) + "\r\n";
}

}

public String Validate(string XMLDoc, string XSDSchema)
{
String ValidationMessage = null;

// Create the XmlSchemaSet class
XmlSchemaSet sc = new XmlSchemaSet();

// Add the schema to the collection
sc.Add(null, XSDSchema);

// Set the validation settings
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler
(ValidationHandler);
settings.ValidationFlags =
XmlSchemaValidationFlags.ReportValidationWarnings |
XmlSchemaValidationFlags.ProcessIdentityConstraints |
XmlSchemaValidationFlags.ProcessInlineSchema |
XmlSchemaValidationFlags.ProcessSchemaLocation;
settings.CloseInput = true;

// Create the XmlReader object
XmlReader reader = XmlReader.Create(XMLDoc, settings);

// Parse the file
while (reader.Read()) ;

if (ErrorsCount > 0)
{
// XML validation failed
ValidationMessage = ErrorMessage;
}
return ValidationMessage;
}
}



[quoted text, click to view]
Martin Honnen
10/18/2007 3:42:56 PM
[quoted text, click to view]

xmlns="urn:mySchemaName" is a namespace declaration, not an
"instruction" to validate against a schema. You should use
xsi:schemaLocation and/or xsi:noNamespaceSchemaLocation attributes if
you want to provide a hint as to which schema(s) an XML instance
document should be validated against.

[quoted text, click to view]

Yes, sure, but how you do that depends on the API of the validating
parser you use. With the .NET framework 2.0 or 3.0 you simply use
XmlReaderSettings and add schemas to its Schemas property e.g. C# pseudo
code
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValidationType = ValidationType.Schema;
readerSettings.ValidationEventHandler += delegate (object sender,
ValdiationEventArgs vargs) {
Console.WriteLine("{0}: {1}", vargs.Severity, vargs.Message);
};
readerSettings.Schemas.Add(null, "schema1.xsd");
using (XmlReader xmlReader = XmlReader.Create("file.xml",
readerSettings))
{
while (xmlReader.Read()) {}
}


--

Martin Honnen --- MVP XML
Martin Honnen
10/18/2007 4:57:36 PM
[quoted text, click to view]

The Add method on XmlSchemaSet
<http://msdn2.microsoft.com/en-us/library/1hh8b082.aspx> takes as the
first argument the targetNamespace and allows the argument to be null
(C#) respectively Nothing (VB.NET) to have the parser use the
targetNamespace as specified in the schema itself.
That way you can validate against any schema without your code to
validate needing to know the schema in advance.


--

Martin Honnen --- MVP XML
Martin Honnen
10/18/2007 5:43:28 PM
[quoted text, click to view]

Validity in terms of schemas is defined based on namespaces, if you have
an element in no namespace and a schema with a target namespace then
that schema does not apply to the instance document and all the
validating parser can emit is a warning that no schema is found.
So your XML instance document needs to have elements in the target
namespace of a schema to be validated against the schema.

--

Martin Honnen --- MVP XML
AddThis Social Bookmark Button