XmlTextReader

S

Slonocode

Is there any relation between the NodeType and the Text when using the
XmlTextReader?

The documentation uses select/case while using the xmltextreader.

While reader.Read()
Select Case reader.NodeType
Case XmlNodeType.Element
Console.Write("<{0}>", reader.Name)
Case XmlNodeType.Text
Console.Write(reader.Value)
Case XmlNodeType.CDATA
Console.Write("<![CDATA[{0}]]>", reader.Value)
Case XmlNodeType.ProcessingInstruction
Console.Write("<?{0} {1}?>", reader.Name,
reader.Value)
Case XmlNodeType.Comment
Console.Write("<!--{0}-->", reader.Value)
Case XmlNodeType.XmlDeclaration
Console.Write("<?xml version='1.0'?>")
Case XmlNodeType.Document
Case XmlNodeType.DocumentType
Console.Write("<!DOCTYPE {0} [{1}]", reader.Name,
reader.Value)
Case XmlNodeType.EntityReference
Console.Write(reader.Name)
Case XmlNodeType.EndElement
Console.Write("</{0}>", reader.Name)
End Select
End While


This would seem to require a great deal of coding to associate an Element
with its Text.

Perhaps I should investigate a different class?

Thanks
Slonocode
 
S

Slonocode

Is there any relation between the NodeType and the Text when using the
XmlTextReader?


Oops. I meant to say...Is there any relation between an Element and it's
Text when using the XmlTextReader?
 
C

Cor

Hi Sonocode,

If you are using only elements, in my opinion the most easy is the dataset
with
ds.writexml(path), after that the xmldocument, with doc.save(path) and if it
then is impossible I would choise the xmltextwriter

Just my opinion

Cor
 
S

Slonocode

Cor said:
Hi Sonocode,

If you are using only elements, in my opinion the most easy is the dataset
with
ds.writexml(path), after that the xmldocument, with doc.save(path) and if it
then is impossible I would choise the xmltextwriter

Just my opinion

Cor

Hello Cor,

I really only need to read an xml feed from a URL and pick out the
information that I want. I don't need to write any xml.

Generic Example:

<Cars>
<Car>Volkswagon</Car>
<Car>Ford</Car>
</Cars>

I need to read the file such that I know that Volkswagon and Ford are cars.

I hope this clarifies it.

Maybe Xpath class would be better?

Slonocode
 
C

Cor

Hi Slonocode

Just use the dataset
dim ds as dataset
ds.readXML(mypath)

You can choise on a lot of methodes to use it but when you first make an
empty XML file it is like this. (There are a lot of alternatives for that, I
hope you find it ok I don't give them all)

<?xml version="1.0" standalone="yes"?>
<MyCartable>
<Cars>
<Car>Volkswagon</Car>
<Colour>Red</Colour>
</Cars>
<Car>
<Car>Ford</Car>
<Colour>Green</Green>
</Cars>
</MyCartable>

MyCartable = the datatable
Cars are the rows
Car = an item (you can have more items)

Than
ds.tables(0).rows(0).items("Volkswagen") = volkswagen
(You can not index rows by name as far as I know).
But that is no problem, when you find a row by its item car you have the
rest

I hope this helps?

Cor
 
J

Jay B. Harlow [MVP - Outlook]

Slonocode,
You can use the following classes to read XML data:
- System.Data.DataSet
- System.Xml.XmlDocument
- System.Xml.XmlTextReader
- System.Xml.XPath.XPathDocument

As Cor suggested the DataSet makes it very easy to read structured (record
set like) data in XML files. If you have recursive data (like named elements
are nested within themselves) datasets do not work. Depending on what you
are doing with the Data, such as putting in a combobox, using a DataSet
makes binding very easy!

The XmlDocument class is good at updatable views of Hierarchial record sets.

The XmlTextReader class is good, if you want total control over the parsing,
which means as you are finding out you need to do ALL the parsing yourself.

For read-only access, I like the XPathDocument class, as you can run XPath
Queries against it to extract your data.

I would recommend either DataSet or XmlDocument if I needed to update the
data. I would recommend DataSet over XmlDocument if I have 'data set' like
data or needed data binding.

I would recommend XPathDocument if I needed Read-Only access to the data and
No databinding was involved. Of course you need to know XPath to effectively
use this class. Note you can use XPath with DataSet's & XmlDocuments also.

I would only use XmlTextReader, when I needed to handle the parsing myself
or needed it in conjunction with one of the above...

Hope this helps
Jay
 
S

Slonocode

Thanks for your help so far Cor and Jay,

Since I need to input a URL as the input for loading the xml I've used the
xmldocument. Then I save the document and I can process it from there.
Then I can get it into a dataset...but thats where I run into trouble. The
dataset ends up with muliple tables with relations. I'm finding it hard to
bind a value to a labels text property if the value is nested in a
relation(If that makes sense).

Here is a sample of the actual xml I need to work with. Maybe you could
tell better what method would work best from that. I'm writing a small
application that will report the weather based on the xml document.

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!--This document is intended only for use by authorized licensees of The
Weather Channel. Unauthorized use is prohibited. Copyright 1995-2002, The
Weather Channel Enterprises, Inc. All Rights Reserved.-->
<weather ver="2.0">
<head>
<locale>en_US</locale>
<form>MEDIUM</form>
<ut>F</ut>
<ud>mi</ud>
<us>mph</us>
<up>in</up>
<ur>in</ur>
</head>
<loc id="48108">
<dnam>Ann Arbor, MI (48108)</dnam>
<tm>2:19 PM</tm>
<lat>42.23</lat>
<lon>-83.72</lon>
<sunr>8:00 AM</sunr>
<suns>5:05 PM</suns>
<zone>-5</zone>
</loc>
<dayf>
<lsup>12/20/03 11:40 AM EST</lsup>
<day d="0" t="Saturday" dt="Dec 20">
<hi>28</hi>
<low>23</low>
<sunr>8:00 AM</sunr>
<suns>5:05 PM</suns>
<part p="d">
<icon>28</icon>
<t>Mostly Cloudy</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>273</d>
<t>W</t>
</wind>
<ppcp>0</ppcp>
<hmid>71</hmid>
</part>
<part p="n">
<icon>27</icon>
<t>Mostly Cloudy</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>218</d>
<t>SW</t>
</wind>
<ppcp>10</ppcp>
<hmid>74</hmid>
</part>
</day>
<day d="1" t="Sunday" dt="Dec 21">
<hi>37</hi>
<low>33</low>
<sunr>8:00 AM</sunr>
<suns>5:06 PM</suns>
<part p="d">
<icon>30</icon>
<t>Partly Cloudy</t>
<wind>
<s>14</s>
<gust>N/A</gust>
<d>222</d>
<t>SW</t>
</wind>
<ppcp>10</ppcp>
<hmid>73</hmid>
</part>
<part p="n">
<icon>26</icon>
<t>Cloudy</t>
<wind>
<s>12</s>
<gust>N/A</gust>
<d>217</d>
<t>SW</t>
</wind>
<ppcp>10</ppcp>
<hmid>78</hmid>
</part>
</day>
<day d="2" t="Monday" dt="Dec 22">
<hi>40</hi>
<low>38</low>
<sunr>8:01 AM</sunr>
<suns>5:06 PM</suns>
<part p="d">
<icon>11</icon>
<t>Few Showers</t>
<wind>
<s>11</s>
<gust>N/A</gust>
<d>224</d>
<t>SW</t>
</wind>
<ppcp>30</ppcp>
<hmid>83</hmid>
</part>
<part p="n">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>5</s>
<gust>N/A</gust>
<d>214</d>
<t>SW</t>
</wind>
<ppcp>40</ppcp>
<hmid>80</hmid>
</part>
</day>
<day d="3" t="Tuesday" dt="Dec 23">
<hi>44</hi>
<low>34</low>
<sunr>8:01 AM</sunr>
<suns>5:07 PM</suns>
<part p="d">
<icon>11</icon>
<t>Showers</t>
<wind>
<s>9</s>
<gust>N/A</gust>
<d>164</d>
<t>SSE</t>
</wind>
<ppcp>40</ppcp>
<hmid>69</hmid>
</part>
<part p="n">
<icon>26</icon>
<t>Cloudy</t>
<wind>
<s>6</s>
<gust>N/A</gust>
<d>128</d>
<t>SE</t>
</wind>
<ppcp>30</ppcp>
<hmid>60</hmid>
</part>
</day>
<day d="4" t="Wednesday" dt="Dec 24">
<hi>38</hi>
<low>24</low>
<sunr>8:02 AM</sunr>
<suns>5:07 PM</suns>
<part p="d">
<icon>16</icon>
<t>Snow</t>
<wind>
<s>12</s>
<gust>N/A</gust>
<d>193</d>
<t>SSW</t>
</wind>
<ppcp>60</ppcp>
<hmid>60</hmid>
</part>
<part p="n">
<icon>27</icon>
<t>Mostly Cloudy</t>
<wind>
<s>10</s>
<gust>N/A</gust>
<d>230</d>
<t>SW</t>
</wind>
<ppcp>30</ppcp>
<hmid>72</hmid>
</part>
</day>
</dayf>
</weather>


Thanks
Slonocode
 
J

Jay B. Harlow [MVP - Outlook]

Slonocode,
All four of these allow loading the XML directly from a URL!
- System.Data.DataSet
- System.Xml.XmlDocument
- System.Xml.XmlTextReader
- System.Xml.XPath.XPathDocument

Any place any of the above accept a file name you can supply a URL.
Here is a sample of the actual xml I need to work with. Maybe you could
tell better what method would work best from that. I'm writing a small
application that will report the weather based on the xml document.
It might be easier if you described what elements in the XML document are
being bound to what fields on your screen.

Hope this helps
Jay
 
C

Cor

Hi Slonocode,

Nothing to add too Jay,

But I think that this one can give you trouble when you are using a dataset.

<day d="1" t="Sunday" dt="Dec 21">

Cor
 
S

Slonocode

Cor said:
Hi Slonocode,

Nothing to add too Jay,

But I think that this one can give you trouble when you are using a dataset.

<day d="1" t="Sunday" dt="Dec 21">

Cor

Yes exactly Cor.

If I try to databind a label to dt I have no way of specifying which dt that
I want

lblDay1.databindings.add("Text", WeatherFeed.ds, "day.dt")

The above will only pull the first one.


I have the xml document open in VS and if I look at the data it is all
related in the table views. I'm unclear how to make this work to my
advantage however.


Slonocode
 
C

Cor

Hi Sonocode,

Did you create a XSD scheme from this XML file?

To do that, you open the XML file in your designer and than do a right
click,
and choose make scheme, you will see that it is not the easiest scheme to
start with.

I never used it this way, but I think it can be done using the Datarelation

I think a real challenge for the first time when you are using a dataset,
therefore I would take first a more simple sample to start with if I was
you.

Cor
 
C

Cor

Hi Slonocode,

Maybe this peace of code helps you I was looking with it to your file, I
never used such a relative complex XML file direct as input for a dataset so
I was curious also.
\\\
Dim ds As New DataSet
ds.ReadXml("c:\test1\slocombe.xml")
For i As Integer = 0 To ds.Tables("day").Rows.Count - 1
MessageBox.Show(ds.Tables("day").Rows(i).Item("dt").ToString & "
" _
& ds.Tables("day").Rows(i).Item("d").ToString & " " _
& ds.Tables("day").Rows(i).Item("t").ToString & " " _
& ds.Tables("day").Rows(i).Item("hi").ToString & " " _
& ds.Tables("day").Rows(i).Item("low").ToString & " " _
& ds.Tables("day").Rows(i).Item("sunr").ToString)
Next
///
Cor
 
J

Jay B. Harlow [MVP - Outlook]

Slonocode,
If I try to databind a label to dt I have no way of specifying which dt that
I want
The above will only pull the first one.

That's because you have a 'table' of day entries. I don't know how you can
bind a "grid of controls" to a single datasource, such that the first row
goes to lblDay1, the second row goes to lblDay2, the third row goes to
lblDay3, and so on... Which is where an XPathDocument and XPath statements
to bind to each individual control. I would consider implementing "binding
to an XPath" as an Extender Provider,
http://msdn.microsoft.com/library/d...de/html/cpconimplementingextenderprovider.asp.

Alternatively you can use either a grid to display all the days in, or use a
listbox to allow selecting which day to display.

Something like:
Dim table As DataTable
table = WeatherFeed.ds.Tables("day")

ListBox1.DataSource = table
ListBox1.DisplayMember = "dt"
ListBox1.ValueMember = "dt"
lblDay1.databindings.add("Text", table, "dt")

Then when you select another day in the ListBox, the date in your lblDay1
will change.

Hope this helps
Jay
 
C

Cor

Hi Jay B

I did not want to write it that deep,

But I think that with a combobox on the table Fday which is the parent of
days and than textboxes for the items from the diferent days that it will
look very nice.

Although

Cor
 
S

Slonocode

Cor said:
Hi Slonocode,

Maybe this peace of code helps you I was looking with it to your file, I
never used such a relative complex XML file direct as input for a dataset so
I was curious also.
\\\
Dim ds As New DataSet
ds.ReadXml("c:\test1\slocombe.xml")
For i As Integer = 0 To ds.Tables("day").Rows.Count - 1
MessageBox.Show(ds.Tables("day").Rows(i).Item("dt").ToString & "
" _
& ds.Tables("day").Rows(i).Item("d").ToString & " " _
& ds.Tables("day").Rows(i).Item("t").ToString & " " _
& ds.Tables("day").Rows(i).Item("hi").ToString & " " _
& ds.Tables("day").Rows(i).Item("low").ToString & " " _
& ds.Tables("day").Rows(i).Item("sunr").ToString)
Next
///
Cor

Thanks Cor,

I have worked with datasets before so I do understand how to navigate a
dataset table as you show above. I guess I'm unclear why I can't databind
to to a particular item. For instance the VS IDE knows that the "part" and
"wind" tables are related to the "day" table. Is there no way to bind a
label to the wind information depending on which day you are looking for?
 
S

Slonocode

That's because you have a 'table' of day entries. I don't know how you
can
bind a "grid of controls" to a single datasource, such that the first row
goes to lblDay1, the second row goes to lblDay2, the third row goes to
lblDay3, and so on... Which is where an XPathDocument and XPath statements
to bind to each individual control. I would consider implementing "binding
to an XPath" as an Extender Provider,
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconimplementingextenderprovider.asp.


Thanks I will study the XPathDocument Class.


Alternatively you can use either a grid to display all the days in, or use a
listbox to allow selecting which day to display.

Something like:
Dim table As DataTable
table = WeatherFeed.ds.Tables("day")

ListBox1.DataSource = table
ListBox1.DisplayMember = "dt"
ListBox1.ValueMember = "dt"

Then when you select another day in the ListBox, the date in your lblDay1
will change.

Hope this helps
Jay


What I'm trying to create is a visual 5 day forecast. So I'd prefer to stay
away from manually having to choose which day to look at. I'd also like to
stay away from a grid for asthetic reasons.
 
C

Cor

Hi Slonocode,

I did also not use this often (I did but you have to do it often to
understand something well I think) But as I stated, the Datarelation I think
should do this, but how with an XSD I do not know, I should have to
investigate as well as you.

And you need probably the binding like this, but I am not sure about it if
that is not overdone when there is a Datarelation.

\\\\
cmaMy = CType(Me.BindingContext(ds.Tables(0)), CurrencyManager)
Me.textbox1.DataBindings.Add(New Binding("Text", ds.tables(0), "lstname"))
////

Cor
 
S

Slonocode

Thanks again Cor and Jay for the all the help.

I have found that the XPath... classes will probably work best for my needs.
Specifically the XPathDocument, XPathNavigator, and XPathNodeIterator.

Very easy to query the xml document for the data needed.

I will probably end up creating a generic class library to interact with
Weather.com xml feeds and then finish up with my small application.

Happy Holidays
Slonocode
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top