473,385 Members | 1,528 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,385 software developers and data experts.

XmlReader problem with XML Fragment

Hi all,

I need a reality check <g>...

I have the following XML fragment:
<event sitename="West Wind Demo Link">
<url>http://rasnotebook/wconnect/testpage.wwd?Test2</url>
<time>11/17/2003 7:32:39 pm</time>
<message>Site is back up now.</message>
</event>
<event sitename="West Wind WebStore">
<url>http://rasnotebook/wconnect/testpage.wwd?Test3</url>
<time>11/17/2003 7:32:43 pm</time>
<message>Site is down.</message>
</event>
I'm having problems parsing this code with an XML Reader. The code I have
(below) runs through the first event and then stops with an exception after
the white space. xr.Read() just fails. For kicks I dumped the doc after
fixup with open and close tags into a DOM and there it works just fine...
XMLReader should be able to deal with a fragment though, so I'm kinda
stumped. Also checked the encoding of the file just to be sure...

The code reads the tag and the white space after it, but on the next read -
failuer with an XmlException...

Any body have any idea what would cause the Read() to just fail? The
document on disk is in fact just an XML fragment not a complete XML doc,
which is why I'm using the XmlReader in the first place (well the file will
get very large too).

.... data table stuff above
XmlTextReader xr = new XmlTextReader(FileName);
DataRow dr = null;

while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Element)
{
if (xr.Name == "event")
{
if (xr.HasAttributes)
{
xr.MoveToAttribute(0);
dr = dt.NewRow();
dr["SiteName"] = xr.Value;
}
}
else if (xr.Name == "url")
dr["Url"] = xr.Value;
else if (xr.Name == "time")
try
{
dr["Time"] = XmlConvert.ToDateTime(xr.Value);
}
catch
{
dr["Time"] = Convert.ToDateTime("01/02/1900");
}
else if (xr.Name == "message")
dr["Message"] = xr.Value;
}
if (xr.NodeType == XmlNodeType.EndElement && xr.Name == "event")
dt.Rows.Add(dr);
}

xr.Close();

return dt;
}

Any ideas?

+++ Rick ---

d Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web

Nov 12 '05 #1
2 2372
Ok, I figured it out...

I was under the impression that the XmlReader didn't really care about a
valid XML document, but apparently the document must still follow valid
snippet options. Once I added a top level doc node it started to work...

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web
"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:#P**************@TK2MSFTNGP12.phx.gbl...
Hi all,

I need a reality check <g>...

I have the following XML fragment:
<event sitename="West Wind Demo Link">
<url>http://rasnotebook/wconnect/testpage.wwd?Test2</url>
<time>11/17/2003 7:32:39 pm</time>
<message>Site is back up now.</message>
</event>
<event sitename="West Wind WebStore">
<url>http://rasnotebook/wconnect/testpage.wwd?Test3</url>
<time>11/17/2003 7:32:43 pm</time>
<message>Site is down.</message>
</event>
I'm having problems parsing this code with an XML Reader. The code I have
(below) runs through the first event and then stops with an exception after the white space. xr.Read() just fails. For kicks I dumped the doc after
fixup with open and close tags into a DOM and there it works just fine...
XMLReader should be able to deal with a fragment though, so I'm kinda
stumped. Also checked the encoding of the file just to be sure...

The code reads the tag and the white space after it, but on the next read - failuer with an XmlException...

Any body have any idea what would cause the Read() to just fail? The
document on disk is in fact just an XML fragment not a complete XML doc,
which is why I'm using the XmlReader in the first place (well the file will get very large too).

... data table stuff above
XmlTextReader xr = new XmlTextReader(FileName);
DataRow dr = null;

while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Element)
{
if (xr.Name == "event")
{
if (xr.HasAttributes)
{
xr.MoveToAttribute(0);
dr = dt.NewRow();
dr["SiteName"] = xr.Value;
}
}
else if (xr.Name == "url")
dr["Url"] = xr.Value;
else if (xr.Name == "time")
try
{
dr["Time"] = XmlConvert.ToDateTime(xr.Value);
}
catch
{
dr["Time"] = Convert.ToDateTime("01/02/1900");
}
else if (xr.Name == "message")
dr["Message"] = xr.Value;
}
if (xr.NodeType == XmlNodeType.EndElement && xr.Name == "event")
dt.Rows.Add(dr);
}

xr.Close();

return dt;
}

Any ideas?

+++ Rick ---

d Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web

Nov 12 '05 #2
See my answer to "Re: XmlDocument read from Network Stream"
It also applies here.

"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Ok, I figured it out...

I was under the impression that the XmlReader didn't really care about a
valid XML document, but apparently the document must still follow valid
snippet options. Once I added a top level doc node it started to work...

+++ Rick ---

--

Rick Strahl
West Wind Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web
"Rick Strahl [MVP]" <ri********@hotmail.com> wrote in message
news:#P**************@TK2MSFTNGP12.phx.gbl...
Hi all,

I need a reality check <g>...

I have the following XML fragment:
<event sitename="West Wind Demo Link">
<url>http://rasnotebook/wconnect/testpage.wwd?Test2</url>
<time>11/17/2003 7:32:39 pm</time>
<message>Site is back up now.</message>
</event>
<event sitename="West Wind WebStore">
<url>http://rasnotebook/wconnect/testpage.wwd?Test3</url>
<time>11/17/2003 7:32:43 pm</time>
<message>Site is down.</message>
</event>
I'm having problems parsing this code with an XML Reader. The code I have (below) runs through the first event and then stops with an exception

after
the white space. xr.Read() just fails. For kicks I dumped the doc after
fixup with open and close tags into a DOM and there it works just fine... XMLReader should be able to deal with a fragment though, so I'm kinda
stumped. Also checked the encoding of the file just to be sure...

The code reads the tag and the white space after it, but on the next

read -
failuer with an XmlException...

Any body have any idea what would cause the Read() to just fail? The
document on disk is in fact just an XML fragment not a complete XML doc,
which is why I'm using the XmlReader in the first place (well the file

will
get very large too).

... data table stuff above
XmlTextReader xr = new XmlTextReader(FileName);
DataRow dr = null;

while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Element)
{
if (xr.Name == "event")
{
if (xr.HasAttributes)
{
xr.MoveToAttribute(0);
dr = dt.NewRow();
dr["SiteName"] = xr.Value;
}
}
else if (xr.Name == "url")
dr["Url"] = xr.Value;
else if (xr.Name == "time")
try
{
dr["Time"] = XmlConvert.ToDateTime(xr.Value);
}
catch
{
dr["Time"] = Convert.ToDateTime("01/02/1900");
}
else if (xr.Name == "message")
dr["Message"] = xr.Value;
}
if (xr.NodeType == XmlNodeType.EndElement && xr.Name == "event")
dt.Rows.Add(dr);
}

xr.Close();

return dt;
}

Any ideas?

+++ Rick ---

d Technologies
http://www.west-wind.com/
http://www.west-wind.com/wwHelp
----------------------------------
Making waves on the Web


Nov 12 '05 #3

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

Similar topics

5
by: xmlguy | last post by:
I believe this should be pretty elementary, but for some reason I cannot seem to think of how to write the an XML file from an incoming XML file. Basically this is what I do: Input: ...
1
by: Jeff | last post by:
Hi I am trying to use the XML Web Control and the XmlReade but when I try and load the xmlReader into the XmlDocument is gives me this erro Error: This document already has a DocumentElement...
2
by: muesliflakes | last post by:
I wish to receive some data from SqlServer as XML and write out result to as a string. Eg: cmd = new SqlCommand( "select * from blah for xml auto", con ); XmlReader result =...
0
by: Senthil Mariappan | last post by:
Hi, I'm getting a wierd error message from VB.Net while using System.Xml.XmlReader. The Code Fragment is like this dim TempReader as System.Xml.XmlReader TempReader = new...
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
1
by: Franck | last post by:
Hi, I'm quit confused with the following problem: I'm tryin to read an Embedded Xml file from my Class Library using a static method which should returns a XmlReader. The point is that using...
2
by: Joe Rattz | last post by:
I am trying to create an XmlReader using XmlReader.Create, but it always returns {None}. I have tried several examples from the web, and Create always returns "{None}". Here is my code: ...
13
by: Alex | last post by:
For example, i have some part of XML file. <AppSettings> <Object ClassVersion="1.0.0.0" Type="AppSettings"> <Fields> <Field Name="App_ID" Type="System.Int32"> <Value> <int>-1</int> </Value>...
0
by: gpet44 | last post by:
Hi, I have a problem receiving XML over a NetworkStream in C# .Net 2.0/3.0. I'm creating an XMLReader from the stream. I get the following exception when the XmlReaderreads from the stream as...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.