chilltemp
asked on
Using XML Schema’s in Visual Studio
I am trying to create an XML Schema (XSD) for use in Visual Studio, but I can not correctly link the file into any XML files. Needless to say, I’m new at XML Schemas; and I’m adding whatever annoyances MS has added to the mix.
Goals:
Create a valid schema file.
Use the file to validate XML documents in Visual Studio
User the file for Visual Studio IntelliSense
The file should not point to an internet address, as it will not be publicly available.
Schema File:
<?xml version="1.0" encoding="utf-8" ?>
<xsi:schema targetNamespace="mySchema. xsd"
elementFormDefault="qualif ied"
xmlns="mySchema.xsd"
xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:mstns="mySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
id="mySchema ">
<xsi:element name="myElement">
XML File:
<?xml version="1.0" encoding="utf-8" ?>
<myElement xmlns=" mySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="//serv er/share/f older/mySc hema.xsd">
Goals:
Create a valid schema file.
Use the file to validate XML documents in Visual Studio
User the file for Visual Studio IntelliSense
The file should not point to an internet address, as it will not be publicly available.
Schema File:
<?xml version="1.0" encoding="utf-8" ?>
<xsi:schema targetNamespace="mySchema.
elementFormDefault="qualif
xmlns="mySchema.xsd"
xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:mstns="mySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema"
id="mySchema ">
<xsi:element name="myElement">
XML File:
<?xml version="1.0" encoding="utf-8" ?>
<myElement xmlns=" mySchema.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="//serv
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks. I was able to get it working in XMLSpy. I guess that MS isn't following the standards 100% (big surprise).
In any case, it's better to validate in code, rather than depending on the parser to do it automatically. For example, just because you've attached a schema to an XML document, there's no guarantee the parser will validate it when it loads. And, since your schema isn't public, you'd have to use some sort of programmatic method anyway.
In C#, this is how I did it recently for an InfoPath web service. This is for a relatively complex schema, that actually consists of four component schemas; one to validate a SOAP message, which imports another to validate the web method, which imports a third that validates the payload of the SOAP message. The third schema includes a fourth that is used as an enumeration for one of the elements in the payload (so I can edit a single simple schema and add new InfoPath forms to my web service).
// Reader for the input stream. Could be from any XML stream
XmlTextReader reader = new XmlTextReader(SoapStreams.
// Need to validate the SOAP message and it's payload
XmlValidatingReader validator = new XmlValidatingReader(reader
// The schemas make use of import and include, so an XmlUrlResolver is needed
XmlUrlResolver ur = new XmlUrlResolver();
// Impersonates the user, but all auth users will have access
ur.Credentials = CredentialCache.DefaultCre
validator.XmlResolver = ur;
// Tells the validator we want to validate using a schema not a DTD
validator.ValidationType = ValidationType.Schema;
// Now create and add the schema
XmlSchema schema = new XmlSchema();
schema = XmlSchema.Read(new XmlTextReader(HttpContext.
validator.Schemas.Add(sche
// Delegate to handle validation errors.
validator.ValidationEventH
// Finally load the validating reader into a DomDocument. Validation checks occur at this point.
xmlDoc.Load(validator);
The validation callback function simply throws an exception if validation fails, but you can do other things instead.
private void ValidationCallBack (object sender, ValidationEventArgs args)
{
throw new Exception("Submitted form is not valid: \r\n" + args.Message);
}
Regards,
Mike Sharp