HighTechTalks DotNet Forums  

How to link xml with xsd "externally"?

Dotnet XML microsoft.public.dotnet.xml


Discuss How to link xml with xsd "externally"? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Eve
 
Posts: n/a

Default How to link xml with xsd "externally"? - 10-18-2007 , 09:33 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?
Thank you!

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: How to link xml with xsd "externally"? - 10-18-2007 , 09:42 AM






Eve wrote:
Quote:
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.

Quote:
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
http://JavaScript.FAQTs.com/


Reply With Quote
  #3  
Old   
Eve
 
Posts: n/a

Default Re: How to link xml with xsd "externally"? - 10-18-2007 , 10:12 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).

"Martin Honnen" wrote:

Quote:
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.

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
http://JavaScript.FAQTs.com/


Reply With Quote
  #4  
Old   
Martin Honnen
 
Posts: n/a

Default Re: How to link xml with xsd "externally"? - 10-18-2007 , 10:57 AM



Eve wrote:
Quote:
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/


Reply With Quote
  #5  
Old   
Eve
 
Posts: n/a

Default Re: How to link xml with xsd "externally"? - 10-18-2007 , 11:31 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.ProcessIdentityConstraint s |
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;
}
}



"Martin Honnen" wrote:

Quote:
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/


Reply With Quote
  #6  
Old   
Martin Honnen
 
Posts: n/a

Default Re: How to link xml with xsd "externally"? - 10-18-2007 , 11:43 AM



Eve wrote:

Quote:
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?
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
http://JavaScript.FAQTs.com/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.