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]
[/code]
[code]
[/code]
Amit ChoudharyPosted Mar 25, 2010, 6:06 AM
Try below code for verifying the xml against xsd:
using System;
using System.Xml;
using System.Xml.Schema;
namespace ConsoleApplication3
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
System.Boolean m_success;
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here.
//
XmlValidatingReader reader = null;
XmlSchemaCollection myschema = new XmlSchemaCollection();
ValidationEventHandler eventHandler = new ValidationEventHandler(Class1.ShowCompileErrors);
try
{
//Create the XML fragment to be parsed.
String xmlFrag = "
"
"
"
//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);
//Implement the reader.
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);
//Add the schema.
myschema.Add("urn:bookstore-schema", "c:\\Books.xsd");
//Set the schema type and add the schema to the reader.
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add(myschema);
while (reader.Read())
{
}
Console.WriteLine("Completed validating xmlfragment");
}
catch (XmlException XmlExp)
{
Console.WriteLine(XmlExp.Message);
}
catch (XmlSchemaException XmlSchExp)
{
Console.WriteLine(XmlSchExp.Message);
}
catch (Exception GenExp)
{
Console.WriteLine(GenExp.Message);
}
finally
{
Console.Read();
}
}
public static void ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine("Validation Error: {0}", args.Message);
}
}
}
Please mark as answer if it helps.
VasanthPosted Mar 25, 2010, 5:04 AM
xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="AddressBook">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="AddressBookEntry">
<xs:complexType>
<xs:sequence>
<xs:element name="Name">
<xs:complexType>
<xs:sequence>
<xs:element name="First" type="xs:string" />
<xs:element minOccurs="0" name="Middle" type="xs:string" />
<xs:element name="Last" type="xs:string" />
xs:sequence>
xs:complexType>
xs:element>
<xs:element minOccurs="0" name="Telephone">
<xs:complexType>
<xs:sequence>
<xs:element name="CountryCode" type="xs:unsignedByte" />
<xs:element name="AreaCode" type="xs:unsignedShort" />
<xs:element name="Exchange" type="xs:unsignedShort" />
<xs:element name="Number" type="xs:unsignedShort" />
xs:sequence>
<xs:attribute name="Type" type="xs:string" use="required" />
xs:complexType>
xs:element>
<xs:element name="EmailAddress" type="xs:string" />
xs:sequence>
xs:complexType>
xs:element>
xs:sequence>
xs:complexType>
xs:element>
xs:schema>
Please mark as answer if its helps you.
SalilPosted Mar 25, 2010, 2:09 AM
Sailaja YPosted Mar 25, 2010, 1:40 AM
Hi,
You can use XmlValidatingReader to validate a XML against XSD Schema. Refer to the below links.
http://msdn.microsoft.com/en-us/library/thydszwy.aspx
http://www.c-sharpcorner.com/uploadfile/jmiguel/xmlschemavalidator11262005021003am/xmlschemavalidator.aspx
Mark it as answer,if it helped you.
Regards,