a n
a n

Reputation:

linq to xml with C# and VS2008

XML Formatted

<status>
 <webresponse>
  <properties>
   <props>
    <imghandle>
     <image handle=1234>
      <imagetag>somewhere</imagetag>
     </image>
    </imghandle>
   </props>
   <props>
    <imghandle>
     <image handle=1235>
      <imagetag>somewhere1</imagetag>
     </image>
    </imghandle>
   </props>
  </properties>
 </webresponse>
</status>

The above is sample of the xml returned to me by a thrid party. I am trying to use Linq to XMl to build my object and I am trying to navigate to <imghandle>/<image handle=1234>. I can do it with Xpath but when i do the following, I get object ref not set error

from c in searchXMLResult.Descendants("props")
select c.Element("imghandle").Element("image").Attribute("handle");

PS: I know my xml is not formatted well for readability, how does one place XML sections when asking a question here?

Update 1: So after much irritating the 3rd party, I finally get an answer saying that the data i was consuming was outdated and they gave me the new formatted data which lookis like this

<status>
     <webresponse>
      <properties>
       <props>
        <imghandle>
         <image handle="1234">
          <imagetag>somewhere</imagetag>
         </image>
        </imghandle>
        <owner>
          <image handle="ABcf">
          </image>
        </owner>
       </props>
       </properties>
     </webresponse>
    </status>

I tried Kevin Thige's suggestion, look at the user suggestions below, and i get an object ref error. Can not having the same element under 2 different sections not work with Linq to XML? i.e

from c in searchXMLResult.Descendants("props")

select c.Element("imghandle").Element("image").Attribute("handle");

and/or

from c in searchXMLResult.Descendants("props")

select c.Element("owner").Element("image").Attribute("handle")

Update2: This is the xml that I am getting and saved to disk

<?xml version="1.0" encoding="utf-8"?>
<status>
  <webresponse>
    <properties>
      <props>
        <imghandle>
          <image handle="537">
            <imagetag>SFO</imagetag>
          </image>
        </imghandle>
        <owner>
          <image handle="User-2">
            <firstname>
            </firstname>
            <lastname>Site Administrator</lastname>
            <username>admin</username>
          </image>
        </owner>
        <creationdate>2009-03-06T18:07:57Z</creationdate>
        <summary>
        </summary>
      </props>
      <status>HTTP/1.1 200 OK</status>
    </properties>
  </webresponse>
</status>

Upvotes: 2

Views: 674

Answers (1)

Kevin Tighe
Kevin Tighe

Reputation: 21171

The code below works for me, once I put quotes around the 'handle' attribute values:

XDocument xdoc = XDocument.Parse(@"
<status>
 <webresponse>
  <properties>
   <props>
    <imghandle>
     <image handle='1234'>
      <imagetag>somewhere</imagetag>
     </image>
    </imghandle>
   </props>
   <props>
    <imghandle>
     <image handle='1235'>
      <imagetag>somewhere1</imagetag>
     </image>
    </imghandle>
   </props>
  </properties>
 </webresponse>
</status>
");

var v = from c in xdoc.Descendants("props")
select c.Element("imghandle").Element("image").Attribute("handle");

EDIT: Without modifying the input to make the xml valid, I don't know what you can do with the XDocument or XmlDocument objects. Both will throw exceptions if you try to load the invalid xml.

EDIT2: The 2nd xml example is still not valid :(. The tag <image handle='ABcf'> is not closed.

Upvotes: 2

Related Questions