Question about using xmlTextReader

  • Thread starter Thread starter Anthony P.
  • Start date Start date
A

Anthony P.

Hello Everyone,

I've not worked a lot with XML but I find myself working on a project
where I need to process a very simply formatted bit of it. After
doing research, I think using xmlTextReader is probably my best way to
go but I've run into a snag I'm hoping someone here can help me with.

First, let's look at the XML I'm processing:

<Response>
<name>David Gibson</name>
<age>27</age>
<date_of_birth>January 27, 2004</date_of_birth>
</response>

Pretty simple, right? So, now, I'm making a simple REST call which
returns that XML in a HTTPWebResponse object called "reader". I then
want to load all of the XML that the web response returns and then
walk it to see which name is returned. Here's what I'm doing:

Dim request as HTTPWebRequest
Dim response as HTTPWebResponse = Nothing
Dim reader as StreamReader
Dim xmlReturnString as String

request = DirectCast(WebRequest.Create("http://localhost/api.php?
record=1123"), HTTPWebRequest)
response = DirectCast(request.GetResponse(), HTTPWebResponse)
reader = new StreamReader(response.GetResponseStream())

'At this point, my reader object should contain the XML. I'll now move
that XML to a string object

xmlReturnString = reader.ReadToEnd()

'Now, I want to use xmlTextReader to check the name.
Dim xmlTxtReader as new xmlTextReader(xmlReturnString)

I'm going to stop here because the line of code above is where the
compiler errors. It tells me "Illegal character in path" but does not
explain why or what.

Can anyone offer me a helping hand here and clue me in as to what I am
doing wrong?

Thanks!
 
After doing research, I think using xmlTextReader is probably my
best way to go but I've run into a snag I'm hoping someone here
can help me with.
First, let's look at the XML I'm processing:
<Response>
<name>David Gibson</name>
<age>27</age>
<date_of_birth>January 27, 2004</date_of_birth>
</response>
reader = new StreamReader(response.GetResponseStream())
xmlReturnString = reader.ReadToEnd()

'Now, I want to use xmlTextReader to check the name.
Dim xmlTxtReader as new xmlTextReader(xmlReturnString)
I'm going to stop here because the line of code above is where the
compiler errors. It tells me "Illegal character in path" but does not
explain why or what.

Two questions:

(1) Read up on the constructor (Sub new) for the XmlTextReader class.
Does it expect a chunk of Xml text or the path to a /file/ containing
same? (Similar to XmlDocument's Load and LoadXml methods).

(2) You did cut-and-paste that example Xml, didn't you?
<*R*esponse>
<name>David Gibson</name>
<age>27</age>
<date_of_birth>January 27, 2004</date_of_birth>
</*r*esponse>

Other things that spring to mind:

Don't store people's age; /calculate/ it as and when you need it; it
does change over time.

If you need to work with parts of any of these fields (say, you needed
to find anyone with a surname starting with "G"), then break these
fields down further. Xml (and particularly Xslt) is great at storing
information, extracting it and bolting bits of it together; it's pretty
rubbish at breaking things apart again, so you might want something like:

<response>
<name surname="Gibson" forenames="David">Gibson, David</name>
<date-of-birth y="2004" m="1" d="27"/>
</response>

The Attributes hold the separated bits that you can search on or extract
and recombine, the "text" bit within each Element can simply be a
"default" format for the same data (obviously, you have to keep the two
in step).

HTH,
Phill W.
 
Back
Top