简体   繁体   中英

C# XmlDocument for grabbing API specific data?

I am trying to grab the specific value of an attribute in:

http://data.alexa.com/data?cli=10&dat=snbamz&url=bing.com

<SD>
<POPULARITY URL="bing.com/" TEXT="16" SOURCE="panel"/>
<REACH RANK="16"/>
<RANK DELTA="-7"/>
<COUNTRY CODE="US" NAME="United States" RANK="9"/>
</SD>
</ALEXA>

I want to grab the value of

I have the current Console Code for this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://data.alexa.com/data?cli=10&dat=snbamz&url=bing.com";
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(url);
            XmlNode root = xmldoc.SelectSingleNode("//@RANK");

            //XmlNamespaceManager xnm1 = new XmlNamespaceManager(xmldoc.NameTable);
            //XmlNodeList nList1 = xmldoc.SelectNodes("//@RANK", xnm1);

            Console.WriteLine(root.ToString());
            Console.ReadLine();


        }
    }
}

But when I run it, I receive the following message in return:

System.Xml.XmlAttribute

What am I doing wrong?

Try changing:

Console.WriteLine(root.ToString());

to:

Console.WriteLine(root.Value);

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM