从REST API获取信息

时间:2014-01-26 21:21:47

标签: c# web-services rest xpath

我正在努力从REST Web服务获取信息。我可以连接,但是当我尝试解析节点时,即使使用命名空间也始终为null:

这是我要解析的XML,我可以成功加载到XML文档中。

<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#" xmlns:ext="http://musicbrainz.org/ns/ext#-2.0" created="2014-01-26T17:20:00.207Z">
    <artist-list count="1" offset="0">
        <artist id="65f4f0c5-ef9e-490c-aee3-909e7ae6b2ab" type="Group" ext:score="100">
            <name>Metallica</name>
            <sort-name>Metallica</sort-name>
            <country>US</country>
            <area id="489ce91b-6658-3307-9877-795b68554c98">
                <name>United States</name>
                <sort-name>United States</sort-name>
            </area>
            <begin-area id="1f40c6e1-47ba-4e35-996f-fe6ee5840e62">
                <name>Los Angeles</name>
                <sort-name>Los Angeles</sort-name>
            </begin-area>
            <life-span>
                <begin>1981-10</begin>
                <ended>false</ended>
            </life-span>
            <alias-list>
                <alias sort-name="メタリカ">メタリカ</alias>
                <alias locale="ko_KR" sort-name="메탈리카" primary="primary">메탈리카</alias>
                <alias sort-name="Metalica">Metalica</alias>
            </alias-list>
            <tag-list>
                <tag count="1">
                    <name>américain</name>
                </tag>
                <tag count="1">
                    <name>usa</name>
                </tag>
                <tag count="2">
                    <name>speed metal</name>
                </tag>
                <tag count="1">
                    <name>douchebag metal</name>
                </tag>
                <tag count="1">
                    <name>american thrash metal</name>
                </tag>
                <tag count="3">
                    <name>rock</name>
                </tag>
                <tag count="1">
                    <name>90s</name>
                </tag>
                <tag count="1">
                    <name>80s</name>
                </tag>
                <tag count="1">
                    <name>seen live</name>
                </tag>
                <tag count="1">
                    <name>rock and indie</name>
                </tag>
                <tag count="10">
                    <name>thrash metal</name>
                </tag>
                <tag count="6">
                    <name>heavy metal</name>
                </tag>
                <tag count="1">
                    <name>thrash</name>
                </tag>
                <tag count="10">
                    <name>metal</name>
                </tag>
                <tag count="1">
                    <name>classic thrash metal</name>
                </tag>
                <tag count="1">
                    <name>classic metal</name>
                </tag>
                <tag count="1">
                    <name>los angeles</name>
                </tag>
                <tag count="1">
                    <name>california</name>
                </tag>
                <tag count="1">
                    <name>bay area</name>
                </tag>
                <tag count="1">
                    <name>80s metal</name>
                </tag>
                <tag count="1">
                    <name>90s metal</name>
                </tag>
                <tag count="1">
                    <name>80s thrash metal</name>
                </tag>
                <tag count="6">
                    <name>american</name>
                </tag>
                <tag count="5">
                    <name>hard rock</name>
                </tag>
                <tag count="1">
                    <name>band</name>
                </tag>
            </tag-list>
        </artist>
    </artist-list>
</metadata>

这是我到目前为止,但名称始终为空

XmlDocument doc = new XmlDocument();
            XPathNavigator nav = doc.CreateNavigator();
            XmlNamespaceManager man = new XmlNamespaceManager(doc.NameTable);
            man.AddNamespace("ext", "http://musicbrainz.org/ns/ext#-2.0");
            doc.LoadXml(textXML);
            XPathNavigator result = nav.SelectSingleNode("/ext:name", man);
            Console.WriteLine(result);

            // Wait for response
            Console.WriteLine("If we get here it is blank");
            Console.ReadLine();
编辑:感谢哈姆雷特,我用Linq解决了这个问题,它有一个更好的语法。对于任何有兴趣的人:

        // Define Namespaces and load XML
        XNamespace def = "http://musicbrainz.org/ns/mmd-2.0#";
        XNamespace ext = "http://musicbrainz.org/ns/ext#-2.0";
        XElement root = XElement.Parse(textXML);

        // Search for required Element
        var elements =
            root
            .Descendants(def + "artist");

        // Print Results
        foreach (var element in elements)
        {
            Console.Write(element.Value);
        }

2 个答案:

答案 0 :(得分:2)

如何使用该网站的json API。您只需要将&fmt=json附加到您的网址并使用json.Net解析json结果

using(var wc = new WebClient())
{
    string url = "http://musicbrainz.org/ws/2/artist?query=%22metallica%22&fmt=json";
    string json = wc.DownloadString(url);
    var metadata = JsonConvert.DeserializeObject<Metadata>(json);

    foreach(var artist in metadata.artists)
    {
        Console.WriteLine(artist.name);
    }
}

public class Area
{
    public string id { get; set; }
    public string name { get; set; }
    [JsonProperty("sort-name")]
    public string sortname { get; set; }
}

public class BeginArea
{
    public string id { get; set; }
    public string name { get; set; }
    [JsonProperty("sort-name")]
    public string sortname { get; set; }
}

public class LifeSpan
{
    public string begin { get; set; }
    public object ended { get; set; }
}

public class Alias
{
    [JsonProperty("sort-name")]
    public string sortname { get; set; }
    public string name { get; set; }
    public string locale { get; set; }
    public string type { get; set; }
    public bool? primary { get; set; }
    [JsonProperty("begin-date")]
    public object begindate { get; set; }
    [JsonProperty("end-date")]
    public object enddate { get; set; }
}

public class Tag
{
    public int count { get; set; }
    public string name { get; set; }
}

public class Artist
{
    public string id { get; set; }
    public string type { get; set; }
    public string score { get; set; }
    public string name { get; set; }

    [JsonProperty("sort-name")]
    public string sortname { get; set; }
    public string country { get; set; }
    public Area area { get; set; }
    [JsonProperty("begin-area")]
    public BeginArea beginarea { get; set; }
    [JsonProperty("life-span")]
    public LifeSpan lifespan { get; set; }
    public List<Alias> aliases { get; set; }
    public List<Tag> tags { get; set; }
}

public class Metadata
{
    public string created { get; set; }
    public int count { get; set; }
    public int offset { get; set; }
    [JsonProperty("artist")]
    public List<Artist> artists { get; set; }
}

答案 1 :(得分:1)

由于默认命名空间中的节点name(没有前缀的命名空间),您必须引用XPath属性local-name()

试试这个:

XmlDocument doc = new XmlDocument();
XPathNavigator nav = doc.CreateNavigator();
doc.LoadXml(textXML);
XPathNavigator result = nav.SelectSingleNode("//*[local-name()='name']");
Console.WriteLine(result);