473,772 Members | 3,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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?Te st2</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?Te st3</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(F ileName);
DataRow dr = null;

while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Ele ment)
{
if (xr.Name == "event")
{
if (xr.HasAttribut es)
{
xr.MoveToAttrib ute(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.ToDa teTime(xr.Value );
}
catch
{
dr["Time"] = Convert.ToDateT ime("01/02/1900");
}
else if (xr.Name == "message")
dr["Message"] = xr.Value;
}
if (xr.NodeType == XmlNodeType.End Element && 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 2403
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********@hot mail.com> wrote in message
news:#P******** ******@TK2MSFTN GP12.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?Te st2</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?Te st3</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(F ileName);
DataRow dr = null;

while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Ele ment)
{
if (xr.Name == "event")
{
if (xr.HasAttribut es)
{
xr.MoveToAttrib ute(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.ToDa teTime(xr.Value );
}
catch
{
dr["Time"] = Convert.ToDateT ime("01/02/1900");
}
else if (xr.Name == "message")
dr["Message"] = xr.Value;
}
if (xr.NodeType == XmlNodeType.End Element && 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********@hot mail.com> wrote in message
news:%2******** ********@TK2MSF TNGP10.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********@hot mail.com> wrote in message
news:#P******** ******@TK2MSFTN GP12.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?Te st2</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?Te st3</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(F ileName);
DataRow dr = null;

while (xr.Read())
{
if (xr.NodeType == XmlNodeType.Ele ment)
{
if (xr.Name == "event")
{
if (xr.HasAttribut es)
{
xr.MoveToAttrib ute(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.ToDa teTime(xr.Value );
}
catch
{
dr["Time"] = Convert.ToDateT ime("01/02/1900");
}
else if (xr.Name == "message")
dr["Message"] = xr.Value;
}
if (xr.NodeType == XmlNodeType.End Element && 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
5644
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: XmlReader instance from another module (beyond my control)
1
1742
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 node It fails at: doc.Load(myXmlReader Is there something I am missing
2
17203
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 = cmd.ExecuteXmlReader( ); Console.Writeln( result.ToString( ) ); This XML is meant to be the input for an XSLT transform so I'm
0
922
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 System.Xml.XmlTextReader (System.IO.MemoryStream) Note, the memory stream has the xml file from the server
1
2314
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: Public Class Form1 Inherits System.Windows.Forms.Form
1
3504
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 DataSet or XmlDocument works perfectly... using XmlReader always fails. I always get "None" as a result. Even using XmlReaderSettings does not change anything.
2
7652
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: XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; settings.IgnoreWhitespace = true; settings.IgnoreComments = true;
13
5771
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> </Field>
0
2451
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 follows while(reader.Read()) { ... } System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 3. Unfortunately I...
0
9620
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10104
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9912
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6715
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.