none
XDocument/XElement can retrieve first but not rest

    Question

  • Hi

    I am a newbie for XDocument/XElement. I can retrieve first element, but fail to retrieve after that. What am I doing wrong?

    Here's the exception information:

    System.NullReferenceException was caught
       Message=Object reference not set to an instance of an object.
       Source=Console_REST_calls1
       StackTrace:
            at Console_REST_calls1.Module1.Main() in c:\... 2010\Projects\Console_REST_calls1\Console_REST_calls1\Module1.vb:line 40

    Here is part of my code, xml follows below.
       Sub Main()
             Dim request As HttpWebRequest
             Dim response As HttpWebResponse = Nothing
             Dim reader As StreamReader

             Dim nsh As XNameitem ="uri"
            Try
                 'create web request
                 request = DirectCast(WebRequest.Create(""), HttpWebRequest)
                 request.Credentials = New NetworkCredential("xxxxxxxx", "xxxxxxxx")
                 'get response
                 response = DirectCast(request.GetResponse(), HttpWebResponse)

                'move response to XDocument
                 reader = New StreamReader(response.GetResponseStream())
                 Dim sxml As String = reader.ReadToEnd
                 Dim xdocument As XDocument = xdocument.Parse(sxml)
                 Dim abc_items As IEnumerable(Of XElement) = xdocument.Elements()

                 Dim numabc_items As Integer = abc_items.Count
                 Console.Write(numabc_items)
                 Dim abc_item As XElement

                ' Read the entire XML
                 For Each abc_item In abc_items.Descendants

                     If Not abc_item.IsEmpty Then
                         Console.WriteLine(abc_item.Descendants(nsh + "item_id").Value.ToString())
                     End If

                Next abc_item

                reader.Close()

            Catch ex As Exception
                 Console.WriteLine(ex.InnerException)
             Finally
                 If Not response Is Nothing Then response.Close()

                 Console.Read()
             End Try
         End Sub

    Part of xml document showing first and second element only. This taken from a watch on abc_items>Results>(0) (<--by the way this is the only occurrence that is loaded.)
    <abc:items xmlns:abc="" xmlns:xl="xxx" pubdate="2013-04-24T11:15:29-04:00">
       <abc:item xl:href="item.xml?item_id=705" id="BRJDNzA1" crc="00000021" status="est">
         <abc:item_id>705</abc:item_id>
         <abc:item_name>PAC150</abc:item_name>
         <abc:formal_name>pac150                                    cap 10</abc:formal_name>
         <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
         <abc:group_name>DON'T USE TBA1</abc:group_name>
         <abc:favorite>F</abc:favorite>
         <abc:max_count>10</abc:max_count>

         <abc:fill_ratio />
         <abc:last_mod_user>user</abc:last_mod_user>
         <abc:last_mod_dt>2007-08-24T14:50:53-04:00</abc:last_mod_dt>
       </abc:item>
       <abc:item xl:href="item.xml?item_id=707" id="BRJDNzA3" crc="00000021" status="est">
         <abc:item_id>707</abc:item_id>

         <abc:item_name>PAC157</abc:item_name>
         <abc:formal_name>pac157                              cap 10</abc:formal_name>
         <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
         <abc:group_name>DON'T USE TBA1</abc:group_name>
         <abc:favorite>F</abc:favorite>
         <abc:max_count>10</abc:max_count>

         <abc:fill_ratio />
         <abc:last_mod_user>user</abc:last_mod_user>
         <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
       </abc:item>

    

    Wednesday, April 24, 2013 6:38 PM

Answers

  • Hi just2curious,

    Since you're using LINQ to XML (with the XDocument and XElement classes), you can use localName + namespace pairs to query certain elements within a given xml document. For your case, the document uses some namespace prefix (abc) for elements in it, you need to specify the namespace qualified element name when querying them. here is a simple example:
     
    #sample document according to the one you provided:

    <?xml version="1.0" encoding="utf-8" ?>
    <abc:items xmlns:abc="urn:test:xml" xmlns:xl="xxx" pubdate="2013-04-24T11:15:29-04:00">
      <abc:item xl:href="item.xml?item_id=705" id="BRJDNzA1" crc="00000021" status="est">
        <abc:item_id>705</abc:item_id>
        <abc:item_name>PAC150</abc:item_name>
        <abc:formal_name>pac150                                    cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:50:53-04:00</abc:last_mod_dt>
      </abc:item>
      <abc:item xl:href="item.xml?item_id=707" id="BRJDNzA3" crc="00000021" status="est">
        <abc:item_id>707</abc:item_id>
    
        <abc:item_name>PAC157</abc:item_name>
        <abc:formal_name>pac157                              cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
      </abc:item>
      <abc:item xl:href="item.xml?item_id=709" id="BRJDNzA5" crc="00000021" status="est">
        <abc:item_id>707</abc:item_id>
    
        <abc:item_name>PAC157</abc:item_name>
        <abc:formal_name>pac157                              cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
      </abc:item>
      <abc:item xl:href="item.xml?item_id=711" id="BRJDNzA7" crc="00000021" status="est">
        <abc:item_id>707</abc:item_id>
    
        <abc:item_name>PAC157</abc:item_name>
        <abc:formal_name>pac157                              cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
      </abc:item>
    </abc:items>
    


    #code for querying all "item" elements in the document (and read attribute and child elements):

       

     Sub ParseXmlContent()
    
            Dim abc_namespace_uri As String = "urn:test:xml"
    
            Dim doc As XDocument = XDocument.Load("TestXML.xml")
    
    
            Dim items As IEnumerable(Of XElement) = doc.Descendants(XName.Get("item", abc_namespace_uri)).ToList()
    
            Console.WriteLine("There are {0} items found.", items.Count())
    
            For Each item In items
    
                Dim id As String = item.Attribute("id").Value
    
                Dim item_id As String = item.Element(XName.Get("item_id", abc_namespace_uri)).Value
                Dim item_name As String = item.Element(XName.Get("item_name", abc_namespace_uri)).Value
    
    
                Console.WriteLine("id:{0}, item_id:{1}, item_name:{2}", id, item_id, item_name)
    
    
            Next
    
        End Sub
    

    For more information about using LINQ to XML, you can take a look at the following references:

    #LINQ to XML
    http://msdn.microsoft.com/en-us/library/bb387098.aspx

    #LINQ to XML with VB.NET
    http://visualbasic.about.com/od/usingvbnet/a/linqXML.htm

    In addition, for XML processing related questions, you can post them in the following forum which is specific to XML related programming issues:

    #XML, System.Xml, MSXML and XmlLite
    http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/threads


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 30, 2013 6:04 AM
    Moderator

All replies

  • Hi, if you want to interate through an XDocument's nodes, you can use foreach statement. Find examples in following posts.

    http://stackoverflow.com/questions/2144962/c-sharp-foreach-xml-node

    http://stackoverflow.com/questions/4989686/how-to-iterate-through-an-xdocuments-nodes

    http://stackoverflow.com/questions/4650277/xml-node-reading-for-each-loop

    Friday, April 26, 2013 10:10 AM
  • Hi just2curious,

    Since you're using LINQ to XML (with the XDocument and XElement classes), you can use localName + namespace pairs to query certain elements within a given xml document. For your case, the document uses some namespace prefix (abc) for elements in it, you need to specify the namespace qualified element name when querying them. here is a simple example:
     
    #sample document according to the one you provided:

    <?xml version="1.0" encoding="utf-8" ?>
    <abc:items xmlns:abc="urn:test:xml" xmlns:xl="xxx" pubdate="2013-04-24T11:15:29-04:00">
      <abc:item xl:href="item.xml?item_id=705" id="BRJDNzA1" crc="00000021" status="est">
        <abc:item_id>705</abc:item_id>
        <abc:item_name>PAC150</abc:item_name>
        <abc:formal_name>pac150                                    cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:50:53-04:00</abc:last_mod_dt>
      </abc:item>
      <abc:item xl:href="item.xml?item_id=707" id="BRJDNzA3" crc="00000021" status="est">
        <abc:item_id>707</abc:item_id>
    
        <abc:item_name>PAC157</abc:item_name>
        <abc:formal_name>pac157                              cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
      </abc:item>
      <abc:item xl:href="item.xml?item_id=709" id="BRJDNzA5" crc="00000021" status="est">
        <abc:item_id>707</abc:item_id>
    
        <abc:item_name>PAC157</abc:item_name>
        <abc:formal_name>pac157                              cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
      </abc:item>
      <abc:item xl:href="item.xml?item_id=711" id="BRJDNzA7" crc="00000021" status="est">
        <abc:item_id>707</abc:item_id>
    
        <abc:item_name>PAC157</abc:item_name>
        <abc:formal_name>pac157                              cap 10</abc:formal_name>
        <abc:group_id xl:href="xxgrp.xml?group_id=31">31</abc:group_id>
        <abc:group_name>DON'T USE TBA1</abc:group_name>
        <abc:favorite>F</abc:favorite>
        <abc:max_count>10</abc:max_count>
    
        <abc:fill_ratio />
        <abc:last_mod_user>user</abc:last_mod_user>
        <abc:last_mod_dt>2007-08-24T14:52:05-04:00</abc:last_mod_dt>
      </abc:item>
    </abc:items>
    


    #code for querying all "item" elements in the document (and read attribute and child elements):

       

     Sub ParseXmlContent()
    
            Dim abc_namespace_uri As String = "urn:test:xml"
    
            Dim doc As XDocument = XDocument.Load("TestXML.xml")
    
    
            Dim items As IEnumerable(Of XElement) = doc.Descendants(XName.Get("item", abc_namespace_uri)).ToList()
    
            Console.WriteLine("There are {0} items found.", items.Count())
    
            For Each item In items
    
                Dim id As String = item.Attribute("id").Value
    
                Dim item_id As String = item.Element(XName.Get("item_id", abc_namespace_uri)).Value
                Dim item_name As String = item.Element(XName.Get("item_name", abc_namespace_uri)).Value
    
    
                Console.WriteLine("id:{0}, item_id:{1}, item_name:{2}", id, item_id, item_name)
    
    
            Next
    
        End Sub
    

    For more information about using LINQ to XML, you can take a look at the following references:

    #LINQ to XML
    http://msdn.microsoft.com/en-us/library/bb387098.aspx

    #LINQ to XML with VB.NET
    http://visualbasic.about.com/od/usingvbnet/a/linqXML.htm

    In addition, for XML processing related questions, you can post them in the following forum which is specific to XML related programming issues:

    #XML, System.Xml, MSXML and XmlLite
    http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/threads


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, April 30, 2013 6:04 AM
    Moderator