C#XmlDocument用于获取API特定数据?

时间:2013-04-15 05:03:09

标签: c# api xpath xmldocument xmlnode

我试图在以下位置获取属性的具体值:

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>

我想抓住

的价值

我有当前的控制台代码:

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();


        }
    }
}

但是当我运行它时,我收到以下消息作为回报:

System.Xml.XmlAttribute

我做错了什么?

1 个答案:

答案 0 :(得分:2)

尝试更改:

Console.WriteLine(root.ToString());

到:

Console.WriteLine(root.Value);

希望有所帮助。