locked
DataSet Xml Schema validation does not work RRS feed

  • Question

  • Hi,

    I am trying to validate the XML that I read into a DataSet using an XML schema (xsd). However, even though the xml file is invalid, I don't get any invalid xml exception. When I print the DataSet contents, it just skips the invalid entry, which is an element named "EmailAddress". In XML schema, the element name is "Email".

    I tried setting EnforceConstraints field of the dataset before reading the XML. But I think it is not related with Schema. It does not change the behavior.

    Here is my code:

    [code]
    using System;
    using System.IO;
    using System.Data;
    using System.Xml;
    using System.Xml.Schema;

    namespace Trial
    {
        public class Program
        {
            static public void Main(String[] args)
            {
                var myDs = new DataSet();

                myDs.ReadXmlSchema("../../AddressBook.xsd");
                myDs.ReadXml("../../AddressBook.xml");
                myDs.AcceptChanges();

                myDs.WriteXml(Console.Out);

                foreach (DataTable table in myDs.Tables)
                {
                    Console.WriteLine("Table {0} ==>", table.TableName);
                    foreach (DataColumn column in table.Columns)
                    {
                        Console.WriteLine("Column {0}", column.ColumnName);
                    }
                }
            }
        }
    }
    [/code]


    Here are my xml and xsd files
    [code]
    <?xml version="1.0"?>

    <!-- 06-xml/05-schema -->

    <AddressBook>

    <AddressBookEntry>

    <Name>

    <First>Niel</First>

    <Middle>M</Middle>

    <Last>Bornstein</Last>

    </Name>

    <Telephone Type="mobile">

    <CountryCode>1</CountryCode>

    <AreaCode>404</AreaCode>

    <Exchange>784</Exchange>

    <Number>0696</Number>

    </Telephone>

    <EmailAddress>niel@bornstein.atlanta.ga.us</EmailAddress>

    </AddressBookEntry>

    <AddressBookEntry>

    <Name>

    <First>Edd</First>

    <Last>Dumbill</Last>

    </Name>

    <EmailAddress>edd@usefulinc.com</EmailAddress>

    </AddressBookEntry>

    </AddressBook>
    [/code]

    [code]
    <?xml version="1.0"?>

    <!-- 06-xml/05-schema -->

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



    <xs:element name="Name">

    <xs:complexType>

    <xs:all>

    <xs:element name="First" type="xs:string" minOccurs="0" />

    <xs:element name="Middle" type="xs:string" minOccurs="0" />

    <xs:element name="Last" type="xs:string" minOccurs="0" />

    </xs:all>

    </xs:complexType>

    </xs:element>



    <xs:element name="Telephone">

    <xs:complexType>

    <xs:sequence>

    <xs:element name="CountryCode" type="xs:string" />

    <xs:element name="AreaCode" type="xs:string" minOccurs="0" />

    <xs:element name="Exchange" type="xs:string" minOccurs="0" />

    <xs:element name="Number" type="xs:string" minOccurs="1"/>

    </xs:sequence>

    <xs:attribute name="Type" type="xs:string" use="required" />

    </xs:complexType>

    </xs:element>



    <xs:element name="Email" type="xs:string" />


    <xs:element name="AddressBookEntry">

    <xs:complexType>

    <xs:sequence>

    <xs:element ref="Name" />

    <xs:element ref="Telephone" minOccurs="0" maxOccurs="unbounded" />

    <xs:element ref="Email" minOccurs="0" maxOccurs="unbounded" />

    </xs:sequence>

    </xs:complexType>

    </xs:element>



    <xs:element name="AddressBook">

    <xs:complexType>

    <xs:sequence>

    <xs:element ref="AddressBookEntry" minOccurs="0" maxOccurs="unbounded" />

    </xs:sequence>

    </xs:complexType>

    </xs:element>


    </xs:schema>

    [/code]
    • Moved by Ji.ZhouModerator Thursday, March 25, 2010 10:51 AM (From:Visual C# General)
    Thursday, March 25, 2010 2:01 AM

Answers