I have an xml like

<?xml version='1.0' encoding='UTF-8'?>
    <feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:docs='http://schemas.google.com/docs/2007' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' gd:etag='W/&quot;C0EARXY8eit7ImA9WhVREE0.&quot;'>
    
    <id>https://docs.google.com/feeds/default/private/full</id>
    <updated>2012-03-17T16:27:24.872Z</updated>
    <category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/docs/2007#item' label='item'/>
    
    <title>Available Documents - </title>
    
    <link rel='alternate' type='text/html' href='https://docs.google.com'/>
    <link rel='http://schemas.google.com/g/2005#resumable-create-media' type='application/atom+xml' href='https://docs.google.com/feeds/upload/create-session/default/private/full'/>
    <link rel='http://schemas.google.com/docs/2007#alt-post' type='application/atom+xml' href='https://docs.google.com/feeds/upload/file/default/private/full'/>
    <link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full'/>
    <link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full'/>
    <link rel='http://schemas.google.com/g/2005#batch' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full/batch'/>
    <link rel='self' type='application/atom+xml' href='https://docs.google.com/feeds/default/private/full/-/document'/>
    
    <some more tags />
    
    <entry gd:etag='&quot;E0UXTh9YDSt7ImBr&quot;'>
    <some more tags />
    <title>hi</title>
    <link rel='http://schemas.google.com/docs/2007#parent' type='application/atom+xml' href=''/>
    <link rel='alternate' type='application/atom+xml' href=''/>
    <link rel='http://schemas.google.com/docs/2007#embed' type='application/atom+xml' href=''/>
    <link rel='http://schemas.google.com/docs/2007#icon' type='application/atom+xml' href=''/>
    <link rel='http://schemas.google.com/g/2005#resumable-edit-media' type='application/atom+xml' href=''/>
    <link rel='http://schemas.google.com/docs/2007#alt-edit-media' type='application/atom+xml' href=''/>
    <link rel='http://schemas.google.com/docs/2007/thumbnail' type='application/atom+xml' href=''/>
    <link rel='self' type='application/atom+xml' href=''/>
    <link rel='edit' type='application/atom+xml' href=''/>
    <link rel='edit-media' type='application/atom+xml' href=''/>
    ...
    <some more tags />
    ...
    </entry>
    <entry>
    ...
    </entry>
    ...

I want to fetch The attribute "href" of the element <link> whose rel="http://schemas.google.com/g/2005#resumable-create-media"

What I'm doing right now is I'm getting list of nodes with xmlnode = doc.GetElementsByTagName("link"); then I'm iterating throught all of them and fetching the first one whose rel="http://schemas.google.com/g/2005#resumable-create-media"

So essentially m looping through all the nodes <link> when I'm just interested in those which are next child of node <feed> and are not inside node <entry>
A <feed> have multiple <entry> nodes which inturn have multiple<link> tags..
And instead I would like to just get the list of <link> which are directly in <feed> and not in <entry>
Any help would be appreciated..

Recommended Answers

All 6 Replies

Hmmmm... it looks like Feed is your document element. This means that it is the highest level node and therefore every other node is a child of it. This being the case, you will need to simply loop through iterate through its child nodes until its child's element name is "entry". If you are able to change the layout of the xml doc, I would put feed as a child to an even higher parent:

<document>
   <feed>...</feed>
   <entry>...</entry>
</document>

This way you can load the feed child node and be sure that you won't have to parse entry nodes if you don't want to. Also, it's more organized to look at.

I cant change the layout as it is a response on Google docs API..

Hmmmm... it looks like Feed is your document element. This means that it is the highest level node and therefore every other node is a child of it. This being the case, you will need to simply loop through iterate through its child nodes until its child's element name is "entry". If you are able to change the layout of the xml doc, I would put feed as a child to an even higher parent:

<document>
   <feed>...</feed>
   <entry>...</entry>
</document>

This way you can load the feed child node and be sure that you won't have to parse entry nodes if you don't want to. Also, it's more organized to look at.

What about this?

using System.Linq;
using System.Xml.Linq;

namespace DW_418207_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         XDocument doc = XDocument.Load("../../XmlFile1.xml");
         
         string strLink = (
            from x in doc.Descendants().First().Elements()
            where x.Name.LocalName.Equals("link")
            && x.Attribute("rel").Value.Contains("resumable-create-media")
            select x.Attribute("rel").Value
         ).FirstOrDefault();

         System.Diagnostics.Debug.WriteLine(strLink);
      }
   }
}

It looks like (and I assumed) "entry" is nested inside "feed".
If you want all of the links, it would not be too hard to modify the query to bring back a List<string>.

commented: Thanx for helping.. Adding Rep :) +2

Worked like a charm.. Can you provide me with some links where I can learn about this technique more..??
Because I'll be needing to do many such fetching for my app..

What about this?

using System.Linq;
using System.Xml.Linq;

namespace DW_418207_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         XDocument doc = XDocument.Load("../../XmlFile1.xml");
         
         string strLink = (
            from x in doc.Descendants().First().Elements()
            where x.Name.LocalName.Equals("link")
            && x.Attribute("rel").Value.Contains("resumable-create-media")
            select x.Attribute("rel").Value
         ).FirstOrDefault();

         System.Diagnostics.Debug.WriteLine(strLink);
      }
   }
}

It looks like (and I assumed) "entry" is nested inside "feed".
If you want all of the links, it would not be too hard to modify the query to bring back a List<string>.

Well, if you search on LINQ to Xml, you will find lots of resources.
Once you know the structure, you can just guess at the code :)

Here is a nice LINQ to XML walk-through.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.