Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Textreader 1

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
Im parsing through a returned httpresponse. I'm looking for a value of ("Typeservice") which I'm getting correctly.
Code:
Dim isdirect as string = nothing

 While reader.Read
           
If reader.MoveToContent() = XmlNodeType.Element And  reader.Name = "TypeService" Then
    isdirect = reader.ReadElementString("TypeService")
 End If

End While


Problem is the element type service is apperaing twice.
I only need the typeservice from the Location Origin role.

- <Location Role="Origin">
<City>ELKTON</City>
<StatePostalCode>TN</StatePostalCode>
<ZipCode>38455</ZipCode>
<NationCode>USA</NationCode>
<Terminal>409</Terminal>
<Zone>2</Zone>
<TypeService>Direct</TypeService>
<AirportCode />
</Location>
- <Location Role="Destination">
<City>Someplace</City>
<StatePostalCode>AZ</StatePostalCode>
<ZipCode>12345</ZipCode>
<NationCode>USA</NationCode>
<Terminal>409</Terminal>
<Zone>2</Zone>
<TypeService>Direct</TypeService>
<AirportCode />
</Location>


How would I refernece it. I'm using

If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "TypeService"

to get the element node I want. I'm not sure how to reference the <Location Role="Origin">

Thanks.
 
Code:
var doc = new XmlDocument();
doc.LoadXml(xml from response);
var serviceNode = doc.SelectNode("\\Location[Role='Origin']\TypeService");
var service = serviceNode.Value;

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
I dont do C# but I think I got the jist of it.

I'm getting an invalid token message with

Dim mynode As XmlNode = xmldoc.SelectSingleNode("\\Location[Role='Origin']\TypeService")


- <Location Role="Origin">
<City>ELKTON</City>
<StatePostalCode>TN</StatePostalCode>
<ZipCode>38455</ZipCode>
<NationCode>USA</NationCode>
<Terminal>409</Terminal>
<Zone>2</Zone>
<TypeService>Direct</TypeService>
<AirportCode />
</Location>
 
Rather than backslash (\), use forwardslash (/).
 
Thanks tsuji

It cleared that error. Now I get a "Object reference not set to an instance of an object." At
Code:
Dim mynode As XmlNode = xmldoc.SelectSingleNode("//Location[Role='Origin']/TypeService")

As I step over the aboce code it shows mynode is set to nothing. I dont think its finding the value but I know its there and I can wath the xmldoc load.


This is the section of the loaded xml document. I can see all the data loaded.
Code:
<Location Role="Origin">
<City>ELKTON</City>
<StatePostalCode>TN</StatePostalCode>
<ZipCode>38455</ZipCode>
<NationCode>USA</NationCode>
<Terminal>409</Terminal>
<Zone>2</Zone>
<TypeService>Direct</TypeService>
<AirportCode></AirportCode>
</Location>
 
>Dim mynode As XmlNode = xmldoc.SelectSingleNode("//Location[Role='Origin']/TypeService")
[tt]Dim mynode As XmlNode = xmldoc.SelectSingleNode("//Location[[red]@[/red]Role='Origin']/TypeService")[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top