Solved

Using XML Schema’s in Visual Studio

Posted on 2004-09-20
4
1,098 Views
Last Modified: 2013-11-19
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="qualified"
  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="//server/share/folder/mySchema.xsd">
0
Comment
Question by:chilltemp
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
J_Mak earned 250 total points
ID: 12107630
Try this:

For your schema:

<?xml version="1.0" encoding="utf-8"?>
<xsd:schema elementFormDefault="qualified"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="myElement">
</xsd:schema>

For your XML file (if your mySchema.xsd file is found in the C:/ drive:

<?xml version="1.0" encoding="utf-8"?>
<myElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/mySchema.xsd"/>

Since you've only defined one element in your schema, your XML file will have one node which is the root node, myElement. If you have XMLShell or XML Spy, this should validate properly against your schema, provided it references its correct location. With the above example, I have assumed that your schema is in your C:/. I haven't tried validating through Visual Studio, but give it a try.

It it not using a target namespace, just to make things easier for you.

Let me know how it went. Cheers!
0
 
LVL 26

Expert Comment

by:rdcpro
ID: 12108560
Well, the only thing inherently wrong with what he was doing was that his schemaLocation attribute does not specify the target namespace. And, he left out the closing schema tag, which I assume was a copy-and-paste typo.  But as far as getting Visual Studio to validate...for some reason it doesn't like the example even though XML Spy validates it.  I would say the Visual Studio IDE is kind of clunky for this anyway.  

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.InputMessage);

// 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.DefaultCredentials;
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.Current.Server.MapPath("schemas/InfoPathSchemas/SoapSchema.xsd")), new ValidationEventHandler(ValidationCallBack));
validator.Schemas.Add(schema, ur);

// Delegate to handle validation errors.
validator.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);

// 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
0
 
LVL 26

Assisted Solution

by:rdcpro
rdcpro earned 100 total points
ID: 12108573
Forgot to post the corrected version of his XML document:

<?xml version="1.0" encoding="UTF-8"?>
<myElement
      xmlns="mySchema.xsd"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
                  mySchema.xsd
                  F:\msharp\PROBON~1\schematest\myschema.xsd"/>

xsi:schemaLocation has two separate strings in it; one for the target namespace (which was missing), and the second one to point to the location of the schema.

Regards,
Mike Sharp
0
 

Author Comment

by:chilltemp
ID: 12123646
Thanks.  I was able to get it working in XMLSpy. I guess that MS isn't following the standards 100% (big surprise).
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Contact Forms for Website 6 66
Detect file exist or not 3 77
JQuery tracking event. 3 39
Grunt No Clean Targets 6 54
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now