views:

89

answers:

2

Hey all

I really didn't want to ask for help as I know I'll eventually figure it out, but I've spent too much time, if the document had parent tags or a better structure, it would be a piece of cake. Sadly I'm downloading the document, and I just can't figure out how to get the data.

I've tried a a few linq queries and a foreach using XElement as an iterator. Anyway here's an example of the structure.

<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:srch" xsi:schemaLocation="urn:yahoo:srch http://api.search.yahoo.com/SiteExplorerService/V1/InlinkDataResponse.xsd" totalResultsAvailable="247930100" firstResultPosition="99" totalResultsReturned="100">
 <Result>
  <Title>Adobe - Adobe Reader</Title> 
  <Url>http://get.adobe.com/fr/reader/&lt;/Url&gt; 
  <ClickUrl>http://get.adobe.com/fr/reader/&lt;/ClickUrl&gt; 
  </Result>
 <Result>
  <Title>Religious Tolerance</Title> 
  <Url>http://www.religioustolerance.org/&lt;/Url&gt; 
  <ClickUrl>http://www.religioustolerance.org/&lt;/ClickUrl&gt; 
  </Result>
 <Result>
  <Title>Applications Internet riches (RIA) | Adobe Flash Player</Title> 
  <Url>http://www.adobe.com/fr/products/flashplayer/&lt;/Url&gt; 
  <ClickUrl>http://www.adobe.com/fr/products/flashplayer/&lt;/ClickUrl&gt; 
  </Result>
 <Result>
  <Title>photo management software | Adobe Photoshop Lightroom 3</Title> 
  <Url>http://www.adobe.com/products/photoshoplightroom/&lt;/Url&gt; 
  <ClickUrl>http://www.adobe.com/products/photoshoplightroom/&lt;/ClickUrl&gt; 
  </Result>
 <Result>
  <Title>Battle for Wesnoth</Title> 
  <Url>http://www.wesnoth.org/&lt;/Url&gt; 
  <ClickUrl>http://www.wesnoth.org/&lt;/ClickUrl&gt; 
  </Result>
</ResultSet>

Here's an example of a latest snippet.

foreach (XElement ele in xDoc.Descendants("ResultSet").Elements("Result"))
                {
                    CollectedUris.Add(ele.Element("Url").Value);
                }
+1  A: 

I'm assuming you want all <Url> elements in the document. If that's the case, then your loop is almost there. You will want to do the following.

using System.Xml.Linq;

foreach (XElement ele in xDoc.Root.Descendants("Result").Descendants("Url")
{
    CollectedUris.Add(ele.Value);
}

Root gets you a reference to the root element, and the following Descendants statement returns only the <Result> nodes. The last Descendants statement further constrains the <Result> node enumerator to only return <Url> elements.

Steve Guidi
Thanks for the example, I've updated the xml structure as it wasn't readable. Anyway weirdly the Add method within the loop isn't fired (got a break point on it) Could you check out the structure and make sure I'm not doing something dumb. Thanks again
Ash
@Ash - see my solution. Steve's is correct if there is not a namespace, but your example requires a namespace manager.
Metro Smurf
+4  A: 

You'll need to add an XNamespace:

XNamespace ns = "urn:yahoo:srch";

var query = xDoc.Root.Descendants( ns + "Result" ).Elements( ns + "Url" )

foreach( XElement e in query )
{
    CollectedUris.Add( e.Value );
}

Edit:
A LINQ solution for bonus points:

xDoc.Root.Descendants( ns + "Result" )
    .Elements( ns + "Url" )
    .Select( x => x.Value ).ToList()
    .ForEach( CollectedUris.Add );
Metro Smurf
Ah I had no idea it about namespaces, certainly wouldn't have solved it without someones help, thanks!
Ash