473,321 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

No rows in dataset when reading in xml?

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>

May 25 '06 #1
0 1655

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tome73 | last post by:
How can I easily add the rows of DataTable1 to the rows of DataTable2. Both queries are from the same table. I can always use the column names with myRow, but I was wishing for a shortcut. When I...
3
by: Diego TERCERO | last post by:
Hi... I'm working on a tool for editing text resources for a family of software product my company produces. These text resources are found in a SQL Server database, in a table called...
3
by: Christoph | last post by:
I'm delving into using ADO.NET for the first time. In fact, I've never done any database work using C# at all so all of this is new to me. Please bear that in mind if I am asking stupid questions....
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
9
by: JFB | last post by:
Hi All, I have a edit flexgrid, after to fill with different rows manually. How can I insert the rows of my flexgrid to my table (sql server) using ado.net? Any example? I can insert one row...
12
by: Graham Blandford | last post by:
Hi all, Would someone be able to tell me the most 'graceful' way of removing unwanted rows from a dataset based on a condition prior to update? OR, resetting the rows all to unchanged after they...
1
by: hzgt9b | last post by:
(FYI, using VB .NET 2003) Can someone help me with this... I'm trying to read in an XML file... it appears to work in that the DataSet ReadXML method dose not fail and then I am able to access the...
4
by: Andre | last post by:
I am ruinning this in the global.asa VIA Visual Studio VB .NET (framework 1.1) I have a access database with a table called tblCounters with a feild called Counters with on row of data in it...
2
by: jed | last post by:
Hello what i have done here is read an xml file into a untyped dataset. the values i can see in a datagrid but how would i gain access to these rows, they are in a txt format in the dataset. Here...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.