invalid xml doesn't throw error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • agda.karlberg@gmail.com

    #1

    invalid xml doesn't throw error

    Hi,

    Apologies if this is a really stupid question, I haven't done much xml
    before.

    I need to validate an xml file against a schema, but when I try to
    validate an invalid file it doesn't throw an error.

    Here's my C# code:

    namespace MyValidationToo l
    {
    class Program
    {
    static void Main(string[] args)
    {
    string xmlFileName = "thexmlfile.xml ";
    string schemaFileName = "theschemafile. xsd";

    XmlReaderSettin gs settings = new XmlReaderSettin gs();
    settings.Valida tionType = ValidationType. Schema;

    XmlSchemaSet schemas = new XmlSchemaSet();
    settings.Schema s = schemas;

    schemas.Add(nul l, schemaFileName) ;

    settings.Valida tionEventHandle r += new
    ValidationEvent Handler(setting s_ValidationEve ntHandler);

    XmlReader validator = XmlReader.Creat e(xmlFileName, settings);

    try
    {
    while (validator.Read ())
    {
    Console.WriteLi ne("test");
    Console.ReadLin e();
    }
    }
    catch (XmlException err)
    {
    Console.WriteLi ne(err.Message) ;
    Console.ReadLin e();
    }
    finally
    {
    validator.Close ();
    }
    }

    static void settings_Valida tionEventHandle r(object sender,
    ValidationEvent Args e)
    {
    Console.WriteLi ne("Validation error: " + e.Message);
    Console.ReadLin e();
    }
    }
    }


    thexmlfile:
    <?xml version="1.0" encoding="UTF-8"?>

    <note xmlns="http://www.w3schools.c om"
    xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
    xs:schemaLocati on="http://www.w3schools.c om theschemafile.x sd">

    <to>Me</to>
    <from>Myself</from>
    <heading>Does it work?</heading>
    <body>Nope, unfortunately not.</body>
    </note>


    theschemafile:
    <?xml version="1.0"?>

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://
    www.w3schools.c om" targetNamespace ="http://www.w3schools.c om"
    elementFormDefa ult="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:string "/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:schema>


    As I said, if I try to break it by making the xml file invalid it
    still doesn't throw an error.

    I'm presuming I'm missing something really obvious here, so I'd really
    appreciate if anoyone who knows why it's happening could please give
    me a pointer in the right direction.

    Thanks,

    AK
  • Jon Skeet [C# MVP]

    #2
    Re: invalid xml doesn't throw error

    <agda.karlberg@ gmail.comwrote:

    <snip>
    As I said, if I try to break it by making the xml file invalid it
    still doesn't throw an error.
    Please give an example of an invalid file, so we can reproduce the
    problem. Otherwise the issue could very easily be your notion of an
    invalid file instead of the code actually being wrong.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Martin Honnen

      #3
      Re: invalid xml doesn't throw error

      agda.karlberg@g mail.com wrote:
      As I said, if I try to break it by making the xml file invalid it
      still doesn't throw an error.
      What exactly did you try to make the file invalid? Show us an XML sample
      that you think is invalid and should cause the ValidationEvent Handler to
      fire.
      The code you have looks ok to me, only you might want to set
      ValidationFlags to ReportValidatio nWarnings as well, see
      http://blogs.msdn.com/xmlteam/archiv...-to-trust.aspx


      --

      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/

      Comment

      • Martin Honnen

        #4
        Re: invalid xml doesn't throw error

        Martin Honnen wrote:
        agda.karlberg@g mail.com wrote:
        >
        >As I said, if I try to break it by making the xml file invalid it
        >still doesn't throw an error.
        >
        What exactly did you try to make the file invalid? Show us an XML sample
        that you think is invalid and should cause the ValidationEvent Handler to
        fire.
        For instance when I change your XML to insert an element not defined in
        your schema

        <note xmlns="http://www.w3schools.c om"
        xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
        xs:schemaLocati on="http://www.w3schools.c om theschemafile.x sd">

        <to>Me</to>
        <from>Myself</from>
        <heading>Does it work?</heading>
        <body>Nope, unfortunately not.</body>
        <foo/>
        </note>

        then your code correctly reports that:

        "Validation error: The element 'note' in namespace
        'http://www.w3schools.c om' has
        invalid child element 'foo' in namespace 'http://www.w3schools.c om'."

        --

        Martin Honnen --- MVP XML
        http://JavaScript.FAQTs.com/

        Comment

        • agda.karlberg@gmail.com

          #5
          Re: invalid xml doesn't throw error

          Turns out it's my debugging skills that was the problem! This is also
          the first time I'm doing a console application, and as I'm writing out
          the below it stops there and doesn't go into the catch block:

          while (validator.Read ())
          {
          Console.WriteLi ne("test");
          Console.ReadLin e();
          }

          If I remove this it throws an error when expected (I broke it by
          removing from one of the tags as well as making one of the elements
          into an int rather than a string, will provide examples next time I
          ask something).

          Thanks for taking the time to look at this, sorry it was only me being
          silly!

          AK

          Comment

          Working...