tags:

views:

307

answers:

1

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>
+2  A: 

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.

Kevin Tighe
I had the same results
Alexander Kahoun
That's great, but the OP doesn't seem to have control over the input. Is there a way to do it without modifying the input?
Whatsit
does the attribute value need to be enclosed with single quotes or would double quotes be suffice. also i do not have conntrol over the data coming to me
@Whatsit - good point. Hmm, without the quotes (single or double) it's not valid xml. I'm not sure what the options are for dealing with invalid xml using XDocument.
Kevin Tighe
Kevin: I followed your sample and did this XDocument searchXMLResult = XDocument.Parse(searchResult.OuterXml.ToString().Trim());var v = from c in xdoc.Descendants("props")select c.Element("imghandle").Element("image");where searchresult is of type XMLDocument. Still gave me an error. I did put double quotes around the attribute values, getting the third party to get me the right xml is like going to the DMV
What error are you getting? Does the XDocument.Parse() line fail or is it the Linq statement?
Kevin Tighe
In the Linq statement, when i expand on the results, I get object ref not set error. also I updated my question and the xml data I am now getting back
The same linq statement worked ok for me with the new xml sample, once I fixed the image tag that wasn't closed. I think the xml you're working with might not be exactly the same as what you posted above. Could you post the raw xml that gets loaded into searchXMLResult?
Kevin Tighe