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" wrote:
> Eve wrote:
> > 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).
>
> 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
>
http://JavaScript.FAQTs.com/
[quoted text, click to view] Eve wrote:
> Right now, in order to validate my xml file against xsd, I include the
> following line in the xml file: xmlns="urn:mySchemaName".
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] > Is it possible to link xml to xsd without specifying the schema inside the
> xml file?
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