Answered Get the value out of XElement object

  • Tuesday, January 08, 2013 10:44 AM
     
     

     Hi all,

    i have a xml structure that has been populated in my XElement  object

    <?xml version="1.0" encoding="utf-8"?>
    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
      <Copyright>Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
      <BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
      <StatusCode>200</StatusCode>
      <StatusDescription>OK</StatusDescription>
      <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
      <TraceId>6bd865ff969c4005b692231bacc5fb6c|LTSM001154|02.00.83.1900|LTSMSNVM002008, LTSMSNVM001478, LTSMSNVM002011, LTSMSNVM001465, LTSMSNVM002402, LTSMSNVM001451, LTSMSNVM002203, LTSMSNVM001479, LTSMSNVM002205, LTSMSNVM001452, LTSMSNVM001466, LTSMSNVM001464, LTSMSNVM002403, LTSM001204, LTSM001211</TraceId>
      <ResourceSets>
        <ResourceSet>
          <EstimatedTotal>1</EstimatedTotal>
          <Resources>
            <Route>
              <Id>v63,i0,a0,cen-US,dAAAAAAAAAAA=,y0,s1,m1,o2,t4,wT1nABPt6zf8=~A+HZ8+sh9HQBAADgAU+6GD8A~TTE0IDVBUiwgTWFuY2hlc3RlciwgTWFuY2hlc3RlciwgVW5pdGVkIEtpbmdkb20=~~~,w3VHjBDJu3P8=~A+HZ8+tR6zcBAADgAcZTDj8C~TkU2IDRQRiwgTmV3Y2FzdGxlIHVwb24gVHluZSwgTmV3Y2FzdGxlIHVwb24gVHluZSwgVW5pdGVkIEtpbmdkb20=~~~,k1</Id>
              <BoundingBox>
                <SouthLatitude>53.452775</SouthLatitude>
                <WestLongitude>-2.305997</WestLongitude>
                <NorthLatitude>54.994801</NorthLatitude>
                <EastLongitude>-1.553171</EastLongitude>
              </BoundingBox>
              <DistanceUnit>Kilometer</DistanceUnit>
              <DurationUnit>Second</DurationUnit>
              <TravelDistance>208.977</TravelDistance>
              <TravelDuration>18995</TravelDuration>  
            </Route>
          </Resources>
        </ResourceSet>
      </ResourceSets>
    </Response>

    i am using bellow code using c#to populate my xml structure

    XElement xmlInfo = XElement.Load("");

    now my question is  how do i get the value out on this element "TravelDistance"  on my above XML structure ?

    thanks



    • Edited by sujee_fdo Tuesday, January 08, 2013 10:44 AM
    •  

All Replies

  • Tuesday, January 08, 2013 11:35 AM
     
     Answered Has Code

    Hi,

    Try this code.

                XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
                XDocument xmlDoc = XDocument.Load(@"D:\Temp\Test1.xml");
                MessageBox.Show(xmlDoc.Descendants(ns + "TravelDistance").FirstOrDefault().Value);
    
                
                //You can use this code too
                XElement xmlInfo = XElement.Load(@"D:\Temp\Test1.xml");
                MessageBox.Show(xmlInfo.Descendants(ns + "TravelDistance").FirstOrDefault().Value);


    Microsoft MVP - ASP/ASP.NET

    • Marked As Answer by sujee_fdo Tuesday, January 08, 2013 3:45 PM
    •  
  • Tuesday, January 08, 2013 2:42 PM
     
     

    Hi Ramesh,

    thanks for u r quick reply, i have another question, for example if my xml file looks like bellow

    <?xml version="1.0" encoding="utf-8"?>
    <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
      <Copyright>Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.</Copyright>
      <BrandLogoUri>http://dev.virtualearth.net/Branding/logo_powered_by.png</BrandLogoUri>
      <StatusCode>200</StatusCode>
      <StatusDescription>OK</StatusDescription>
      <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
      <TraceId>4d5299cbe7fe493dab5518eee35d1396|PRAM001258|02.00.83.1900|PRAMSNVM001165, PRAMSNVM001169, PRAMSNVM001170, PRAIPEVM000019, PRAMSNVM001906, PRAMSNVM001172, PRAMSNVM001158, PRAMSNVM001905, PRAMSNVM002051, PRAMSNVM001156, PRAMSNVM002052, PRAIPEVM000036, PRAMSNVM001174, PRAMSNVM001903, PRAIPEVM000014, PRAMSNVM001155, PRAIPEVM000015, PRAM000031, PRAM000013, PRAM000012, PRAM000039</TraceId>
      <ResourceSets>
        <ResourceSet>
          <EstimatedTotal>1</EstimatedTotal>
          <Resources>
            <Route>
              <Id>v63,i0,a0,cen-US,dAAAAAAAAAAA=,y0,s1,m1,o1,t4,wT1nABPt6zf8=~A+HZ8+sh9HQBAADgAU+6GD8A~TTE0IDVBUiwgTWFuY2hlc3RlciwgTWFuY2hlc3RlciwgVW5pdGVkIEtpbmdkb20=~~~,w3VHjBDJu3P8=~A+HZ8+tR6zcBAADgAcZTDj8C~TkU2IDRQRiwgTmV3Y2FzdGxlIHVwb24gVHluZSwgTmV3Y2FzdGxlIHVwb24gVHluZSwgVW5pdGVkIEtpbmdkb20=~~~,wT1nABPt6zf8=~A+HZ8+sh9HQBAADgAU+6GD8A~TTE0IDVBUiwgTWFuY2hlc3RlciwgTWFuY2hlc3RlciwgVW5pdGVkIEtpbmdkb20=~~~,w3lyqBFP41P8=~A+HZ8+uxrHwCAADgAQAAAAAC~QjQgN0VTLCBVbml0ZWQgS2luZ2RvbQ==~~~,k0</Id>
              <BoundingBox>
                <SouthLatitude>52.485182</SouthLatitude>
                <WestLongitude>-2.41755</WestLongitude>
                <NorthLatitude>55.009779</NorthLatitude>
                <EastLongitude>-1.337972</EastLongitude>
              </BoundingBox>
              <DistanceUnit>Mile</DistanceUnit>
              <DurationUnit>Second</DurationUnit>
              <TravelDistance>398.280914</TravelDistance>
              <TravelDuration>26202</TravelDuration>
              <RouteLeg>
                <TravelDistance>159.675619</TravelDistance>
                <TravelDuration>10119</TravelDuration>
                <StartTime>0001-01-01T00:00:00</StartTime>
                <EndTime>0001-01-01T00:00:00</EndTime>
              </RouteLeg>
              <RouteLeg>
                <TravelDistance>152.847993</TravelDistance>
                <TravelDuration>10239</TravelDuration>     
                <StartTime>0001-01-01T00:00:00</StartTime>
                <EndTime>0001-01-01T00:00:00</EndTime>
              </RouteLeg>
              <RouteLeg>
                <TravelDistance>85.757302</TravelDistance>
                <TravelDuration>5844</TravelDuration>    
                <StartTime>0001-01-01T00:00:00</StartTime>
                <EndTime>0001-01-01T00:00:00</EndTime>
              </RouteLeg>
            </Route>
          </Resources>
        </ResourceSet>
      </ResourceSets>
    </Response>

    how do i get the <TravelDistance>  value under each <RouteLeg>  ?

    i need to get each values like

    159.675619

    152.847993

    85.757302

    thanks

  • Tuesday, January 08, 2013 3:13 PM
     
     Answered Has Code

    Hi sujee_fdo;

    I showed two queries below one returns a IEnumerable of string's and the other returns a IEnumerable of float's

    XDocument xdoc = XDocument.Load("C:/Working Directory/test1.xml");
    XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";
    
    // This query will return a IEnumerable<String>
    var results = from rl in xdoc.Descendants(ns + "RouteLeg")
                  select rl.Element(ns + "TravelDistance").Value;
                  
    // If you want the query to return an IEnumerable<float> then use this query
    var results = from rl in xdoc.Descendants(ns + "RouteLeg")
                  select (float)rl.Element(ns + "TravelDistance");

      


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Marked As Answer by sujee_fdo Tuesday, January 08, 2013 3:45 PM
    •