Hi,
Ive had a bit of a look around for other people with this problem and
cant find anything that solves it in my case, so I'm afraid im going to
have to bother you all with a post of my own.
Essentially, I am trying to read in an xml schema and then an xml
document into a dataset using ReadXmlSchema and then ReadXml. When i do
this, i end up with a table in the dataset (which i assume is created
when i read the schema), but no rows representing my document.
I read in a couple of places that validation failing against the schema
would cause data from my xml file, so i validated my document against
the schema using a ValidatingReader. This produced a few problems which
i have now fixed (or, at least, the document passes validation), but i
still do not get any rows added to my dataset when i call ReadXml.
Im sure its probably something simple that i have neglected to do, but
im pretty stumped - it certainly seems like i should only have to call
the two Read methods on the dataset without doing anything else from
what i understand from the documentation -, so any help getting this
sorted would be much appreciated. Sorry if this is the wrong NG, it was
a toss up between this and the ADO NG, and I didnt want to xpost - if
it let me know and ill repost over there.
The code that is causing the problem is below.
Many thanks in advance for the help,
Chris
using System;
using System.Xml;
using System.Xml.Schema;
using System.Data;
namespace XmlValidator
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Validator
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
try
{
string schemaPath = args [0];
string xmlFilePath = args [1];
if (schemaPath.Length <= 0)
{
Validator.DoUsage ();
return;
}
if (xmlFilePath.Length <= 0)
{
Validator.DoUsage ();
return;
}
//check that the files exist
if (System.IO.File.Exists (schemaPath) == false)
{
Console.WriteLine ("Schema File not found - please supply a fully
qualified path name");
Console.ReadLine ();
return;
}
//check that the files exist
if (System.IO.File.Exists (xmlFilePath) == false)
{
Console.WriteLine ("Xml File not found - please supply a fully
qualified path name");
Console.ReadLine ();
return;
}
//get a validating reader set up
XmlValidatingReader vreader = Validator.GetReader (schemaPath,
xmlFilePath);
while (vreader.Read ())
{
Console.WriteLine (vreader.NodeType.ToString()); //loop through
}
Console.WriteLine ("Validation Complete");
Console.ReadLine ();
//get a dataset set up
DataSet ds = new DataSet ();
ds.ReadXmlSchema (schemaPath);
ds.ReadXml (xmlFilePath, XmlReadMode.ReadSchema);
Console.WriteLine (ds.Tables["Request"].Rows.Count.ToString ());
Console.ReadLine ();
}
catch (Exception ex)
{
Console.WriteLine (ex.ToString ());
Console.ReadLine ();
}
}
private static XmlValidatingReader GetReader (string schemaPath,
string xmlFilePath)
{
try
{
XmlTextReader schemaReader = new XmlTextReader (schemaPath);
XmlSchemaCollection schemas = new XmlSchemaCollection ();
schemas.Add (null, new XmlTextReader (schemaPath));
XmlTextReader fileReader = new XmlTextReader (xmlFilePath);
XmlValidatingReader r = new XmlValidatingReader (fileReader);
r.Schemas.Add (schemas);
r.ValidationType = ValidationType.Auto;
r.ValidationEventHandler +=new
System.Xml.Schema.ValidationEventHandler(r_Validat ionEventHandler);
return r;
}
catch (Exception ex)
{
throw ex;
}
}
private static void PrintUsage ()
{
Console.WriteLine ("Usage: XmlValidator [schema] [xml file]");
Console.ReadLine ();
}
private static void r_ValidationEventHandler(object sender,
System.Xml.Schema.ValidationEventArgs e)
{
Console.WriteLine (e.Exception.ToString ());
}
}
}
SCHEMA:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="CheckStatusRequest"
targetNamespace="http://tempuri.org/CheckStatusRequest.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/CheckStatusRequest.xsd"
xmlns:mstns="http://tempuri.org/CheckStatusRequest.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Request">
<xs:complexType>
<xs:sequence />
<xs:attribute name="Type" type="xs:string" use="required" />
<xs:attribute name="SequenceNumber" type="xs:integer" use="required"
/>
<xs:attribute name="RequesterStationID" type="xs:integer"
use="optional" />
<xs:attribute name="RequesterLocationID" type="xs:integer"
use="optional" />
</xs:complexType>
</xs:element>
</xs:schema>
SAMPLE DOCUMENT
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Request xmlns="http://tempuri.org/CheckStatusRequest.xsd"
Type="CheckStatus"
SequenceNumber="12345"
RequesterStationID="123"
RequesterLocationID="456"></Request>