0

I have an xml file that has two links in it. I need to check to see if the link with the rel next exists if it does get the href value of it.

<a:link rel="prev" type="application/atom+xml" type="application/atom+xml" href="/v3.2/en-us/" />
<a:link rel="next" type="application/atom+xml" type="application/atom+xml" href="/v3.2/en-us/" />
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
scripter78
  • 1,117
  • 3
  • 22
  • 50
  • Can you post an example of what you have so far? – Jake1164 Jun 08 '12 at 14:46
  • Sorry the most i have gotten is string test = ReturnedAppsXml.Element(ns + "link").Attribute(rel).Value; that returns the value of the first link. How other than looping can I check either with regex or what not if the second link exists with the rel value of next – scripter78 Jun 08 '12 at 14:48
  • That's not valid Xml with two type attributes. – Chuck Savage Jun 08 '12 at 15:38

2 Answers2

2

How about reading the xml into an XDocument and using LINQ to find the next element.

XDocument x = XDocument.Parse("<xml><link rel=\"prev\" type=\"application/atom+xml\" href=\"/v3.2/en-us/\" /> <link rel=\"next\" type=\"application/atom+xml\" href=\"/v3.2/en-us/\" /></xml>");

XElement link = x.Descendants("link")
                 .FirstOrDefault(a => a.Attribute("rel").Value == "next");

String href = string.Empty;
if(link != null)
{
     href = link.Attribute("href").Value; 
}
Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
Jake1164
  • 12,291
  • 6
  • 47
  • 64
  • For simplicity I removed the namespace, here is a link to a question about reading xml with namespaces that might come in handy as well. http://stackoverflow.com/questions/8996300/search-xml-element-with-xml-namespace-prefix-c-sharp – Jake1164 Jun 08 '12 at 15:11
  • This will fail (exception will be thrown) when `.FirstOrDefault` returns the default value of `null` when no value is found for the "rel" attribute. – Metro Smurf Jun 08 '12 at 15:26
  • Correct, it will throw exceptions if any of the items are missing. I would recommend validating the xml using a schema first. – Jake1164 Jun 08 '12 at 15:34
  • Even if it validates, the `FirstOrDefault` will cause an exception if the rel attribute (or value) does not exist; there's no requirement that the value must exist. From the OP: _I need to check to see if the link with the rel next exists if it does get the href value of it._ – Metro Smurf Jun 08 '12 at 16:13
0

You can use this public xml library and then get the value with:

XElement root = XElement.Load(file); // or .Parse(string)
string href = root.XGetElement<string>("//a:link[@rel={0}]/href", null, "next");
if(null != href)
    ... link was found ...

That should work with the a:link, but if it doesn't try it without the a:. It would depend on where a's namespace was declared. If it is in the root node it should be fine.

XGetElement() is basically a combination of XPathElement("//a:link[@rel={0}]").Get<string>("href", null). Get() also being a part of the library for getting values from nodes, checking first for an attribute by the name and then for a child node.

Chuck Savage
  • 11,775
  • 6
  • 49
  • 69