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!

XMLreader skipping elements 1

Status
Not open for further replies.

Dashley

Programmer
Dec 5, 2002
925
US
This is really annoying the bageebers out of me.
When running the reader is skiping every other element.
City and Zip5 are not being assigned values.

I've even added:
If reader.NodeType = XmlNodeType.Element ...
and
If reader.NodeType = XmlNodeType.Element And reader.NodeType <> XmlNodeType.EndElement ...

Any Ideas?

Thanks

Code:
' Returned xml
          <?xml version="1.0" ?> 
          - <AddressValidateResponse>
            - <Address ID="1">
              <Address2>8 WILDWOOD DR</Address2> 
              <City>OLD LYME</City> 
              <State>CT</State> 
              <Zip5>06371</Zip5> 
              <Zip4>1844</Zip4> 
              </Address>
            </AddressValidateResponse>

Code:
Dim Address As String = Nothing
Dim City As String = Nothing
Dim state As String = Nothing
Dim zip5 As String = Nothing
Dim zip4 As String = Nothing

Dim reader As XmlTextReader = New XmlTextReader("asuperlongurl")

reader.WhitespaceHandling = WhitespaceHandling.None

 While reader.Read
           
Select Case reader.Name
  Case "Address2" : Address = reader.ReadElementString
  Case "City" : City = reader.ReadElementString
  Case "State" : state = reader.ReadElementString
  Case "Zip5" : zip5 = reader.ReadElementString
  Case "Zip4" : zip4 = reader.ReadElementString
End Select
          

End While
 
try using the overloaded method of ReadElementString
reader.ReadElementString("City") ... etc.
 
Hi jbenson001

I just tried the overload method and no luck.
As I step through with the debugger its not even picking up
the City or Zip5 nodes when if pass over the Select Case reader.Name. This is driving me nuts.
 
jbenson001

I can get it to work by using reader.MoveToContent().
It'll do fo rnow. I'm still wondering why it was skipping.



If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "Address2" Then Address = reader.ReadElementString("Address2")

If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "City" Then City = reader.ReadElementString("City")

If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "State" Then state = reader.ReadElementString("State")

If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "Zip5" Then zip5 = reader.ReadElementString("Zip5")

If reader.MoveToContent() = XmlNodeType.Element And reader.Name = "Zip4" Then zip4 = reader.ReadElementString("Zip4")
 
>I'm still wondering why it was skipping.
The method ReadElementString() you keep using has its proper behaviour, namely, after it's done, the position of the reader is _after_ the EndElement. It is _after_. Hence, in the loop testing reader.Read(), it is positioned at the text node already. Hence, it skip every other element after the Address2 element is reached.

To properly salvage the original layout, you can use ReadString() method, and it would be fine.
[tt]
Select Case reader.Name
Case "Address2" : Address = reader.ReadString()
Case "City" : City = reader.ReadString()
Case "State" : state = reader.ReadString()
Case "Zip5" : zip5 = reader.ReadString()
Case "Zip4" : zip4 = reader.ReadString()
End Select
[/tt]
 
tsuji,

Thank you. That makes perfect sense. I appreciate your taking the time to expplaint.

take care

-dan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top