Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Xml validation. Please help.

Xml validation. Please help.

Scheduled Pinned Locked Moved C#
xmldatabasehelpquestionannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    paper67
    wrote on last edited by
    #1

    I want to validate an XML file through a schema.

    <?xml version="1.0" encoding="utf-8" ?>

    <Units>
    <Unt Text="stuks" Service="False" Value="0" />
    <Unit Text="km" Service="True" Value="1" />
    </Units>

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="Unit" targetNamespace="http://tempuri.org/Unit.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/Unit.xsd"
    xmlns:mstns="http://tempuri.org/Unit.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:complexType name="UnitType">
    <xs:attribute name="Text" type="xs:string" use="required" />
    <xs:attribute name="Service" type="xs:boolean" use="required" />
    <xs:attribute name="Value" type="xs:byte" use="required" />
    </xs:complexType>

    <xs:element name="Units">
    xs:complexType
    <xs:sequence minOccurs="0" maxOccurs="unbounded">
    <xs:element name="Unit" type="UnitType" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    </xs:schema>

    I use the following code :

    Settings = new XmlReaderSettings();
    Settings.ValidationType = ValidationType.Schema;
    Settings.Schemas.Add("http://tempuri.org/Unit.xsd", Directory.GetCurrentDirectory() + strXmlSchema);
    Settings.ValidationEventHandler += new ValidationEventHandler(OnSchemaValidationError);

    using (XmlReader Reader = XmlReader.Create(Directory.GetCurrentDirectory() + strXmlFile, Settings))
    {
    if (Reader != null)
    {
    XmlDocument Doc = new XmlDocument();
    Doc.Load(Reader);
    Reader.Close();
    :
    }
    }

    My handler OnSchemaValidationError never gets called, even when there is clearly a mistake in my XML. Why ??

    S 1 Reply Last reply
    0
    • P paper67

      I want to validate an XML file through a schema.

      <?xml version="1.0" encoding="utf-8" ?>

      <Units>
      <Unt Text="stuks" Service="False" Value="0" />
      <Unit Text="km" Service="True" Value="1" />
      </Units>

      <?xml version="1.0" encoding="utf-8"?>
      <xs:schema id="Unit" targetNamespace="http://tempuri.org/Unit.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/Unit.xsd"
      xmlns:mstns="http://tempuri.org/Unit.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">

      <xs:complexType name="UnitType">
      <xs:attribute name="Text" type="xs:string" use="required" />
      <xs:attribute name="Service" type="xs:boolean" use="required" />
      <xs:attribute name="Value" type="xs:byte" use="required" />
      </xs:complexType>

      <xs:element name="Units">
      xs:complexType
      <xs:sequence minOccurs="0" maxOccurs="unbounded">
      <xs:element name="Unit" type="UnitType" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>

      </xs:schema>

      I use the following code :

      Settings = new XmlReaderSettings();
      Settings.ValidationType = ValidationType.Schema;
      Settings.Schemas.Add("http://tempuri.org/Unit.xsd", Directory.GetCurrentDirectory() + strXmlSchema);
      Settings.ValidationEventHandler += new ValidationEventHandler(OnSchemaValidationError);

      using (XmlReader Reader = XmlReader.Create(Directory.GetCurrentDirectory() + strXmlFile, Settings))
      {
      if (Reader != null)
      {
      XmlDocument Doc = new XmlDocument();
      Doc.Load(Reader);
      Reader.Close();
      :
      }
      }

      My handler OnSchemaValidationError never gets called, even when there is clearly a mistake in my XML. Why ??

      S Offline
      S Offline
      simplefolk
      wrote on last edited by
      #2

      I beleive you have to read the entire file in order for it to validate try this

      Settings = new XmlReaderSettings();
      Settings.ValidationType = ValidationType.Schema;
      Settings.Schemas.Add("http://tempuri.org/Unit.xsd", Directory.GetCurrentDirectory() + strXmlSchema);
      Settings.ValidationEventHandler += new ValidationEventHandler(OnSchemaValidationError);

      using (XmlReader Reader = XmlReader.Create(Directory.GetCurrentDirectory() + strXmlFile, Settings))
      {
      if (Reader != null)
      {
      while(Reader.Read()){}
      }
      }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups