views:

47

answers:

3

I am developing a system that will receive a XML (XmlDocument) via webservice. I won't have this XML (XmlDocument) on hardisk. It will be managed on memory.

I have a file XSD to validate the XML (XmlDocument) that I receive from my WebService. I am trying to do a example to how validate this Xml.

My XML:

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

also I have my XSD:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
  <xs:element name="note">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="to" type="xs:string"/>
        <xs:element name="from" type="xs:string"/>
        <xs:element name="heading" type="xs:string"/>
        <xs:element name="body" type="xs:int"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

As we can see, the body field I've put as int, just to simulate the error.

Well, to try get the error, I have the following code:

//event handler to manage the errors
private static void verifyErrors(object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Warning)
        MessageBox.Show(args.Message);
}

On click button, I have:

        private void button1_Click(object sender, EventArgs e)
        {

            try
            {
                // my XmlDocument (in this case I will load from hardisk)
                XmlDocument xml = new XmlDocument();
                // load the XSD schema.. is this right?
                xml.Schemas.Add("http://www.w3schools.com", "meuEsquema.xsd");

                // Load my XML from hardisk
                xml.Load("meusDados.xml");

                // event handler to manage the errors from XmlDocument object
                ValidationEventHandler veh = new ValidationEventHandler(verificaErros);

                // try to validate my XML.. and the event handler verifyError will show the error
                xml.Validate(veh);
            }
            catch {
              // do nothing.. just to test
            }
        }

The problem is that I changed the body field to int, but there are a string value in that field and I am not getting error.

A: 

The problem is XML namespaces.

In your XSD, you define the targetNamespace= and xmlns= to both be "http://www.w3schools.com":

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.w3schools.com"
           xmlns="http://www.w3schools.com"
           elementFormDefault="qualified">

However - your XML document does not contain any XML namespaces.

<?xml version="1.0"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

So basically, that XSD isn't validating this XML at all.

You need to either remove those namespaces from your XSD:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">

or alternatively, add the default XML namespace (with no prefix) defined in your XSD to your XML:

<?xml version="1.0"?>
<note xmlns="http://www.w3schools.com"&gt;
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>

If you have XML namespaces in your XSD, those have to be present in the XML as well - and vice versa.

Once you do one or the other solution, you should get a validation error something like:

Validation error: The 'body' element is invalid - The value 'Don't forget me this weekend!' is invalid according to its datatype
'http://www.w3.org/2001/XMLSchema:int' - The string 'Don't forget me this weekend!' is not a valid Int32 value.
marc_s
Hi Marc,I've tried to put the namespace but it didn't work.My namespace:<note xmlns:xsi="http://www.w3.org/2001/XMLSchema" xsi:noNamespaceSchemaLocation="./Schema.xsd">What am I doing wrong?
Dan
@Dan: use the version I posted in my response: `<note xmlns="http://www.w3schools.com">`
marc_s
Hi @marc_s.Can you help me with this solution for 10 dollars by paypal? I really didn't get the error.I know you're here voluntarily, But I am a new programmer in c# and I am still studing XML.Best,Dan
Dan
@Dan: send me an e-mail - see my profile - I'll try to help via e-mail, probably easier and more efficient than here.
marc_s
A: 

Hi Dan, I am hoping I can help you here. I had a similar issue (with Xml false-positives).

I found two errors in my particular situation, one which may actually be relevant. You must opt-in to various Xml validation via XmlReaderSettings. Here is a simple usage (taken from post above)

string schemaFileName = @"sampleSchema.xsd"; 
string xmlFileName = @"sampleXml.xml"; 
XmlReaderSettings settings = new XmlReaderSettings 
{ 
    ValidationType = ValidationType.Schema, 
    ValidationFlags =  
        XmlSchemaValidationFlags.ProcessInlineSchema | 
        XmlSchemaValidationFlags.ProcessSchemaLocation |  
        XmlSchemaValidationFlags.ReportValidationWarnings, 
}; 
settings.Schemas.Add (schema); 
settings.ValidationEventHandler +=  
    (o, e) => { throw new Exception("CRASH"); }; 

XmlSchema schema =  
    XmlSchema.Read ( 
    File.OpenText (schemaFileName),  
    (o, e) => { throw new Exception ("BOOM"); }); 

// obviously you don't need to read Xml from file, 
// just skip the load bit and supply raw DOM object
XmlReader reader = XmlReader.Create (xmlFileName, settings); 
while (reader.Read ()) { } 
johnny g
That won't be addressing the real problem - XML namespaces defined in the XSD, but not present in the XML.....
marc_s
A: 

Thank you guys,

I will try that.

Thank you very much.

Best regards, Dan

Dan
hey Dan, best of luck with your issue - though as a side note, if an answer helped with your problem please upvote (left clicking the up arrow next to a response) and if one answer is more relevant or resolved your problem directly, please accept (left clicking the check mark next to a response). muchos gracias!
johnny g