Can't get inner text from FQL using Xpath C#

一世执手 提交于 2020-01-06 19:39:10

问题


I'm trying to get the inner text from a XML tag called sharecount, I need to get it by the normalized_url tag like so:

    string ShareCount;
    string Url = "'" + "http://www.example.com/Article.aspx?id=" + GuideID + "'";
    XmlNode Node;
    try
    {
        //return Share count (using Xpath). 
        XmlDoc.SelectSingleNode("fql_query_response/link_stat[normalized_url=" + Url + "]/share_count");
        ShareCount = XmlDoc.InnerText;
        int.TryParse(ShareCount, out Value); //Turn string to int.
        return Value;
    }
    catch
    {
        return 0;
    }

and this is the XML:

<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
<link_stat>
<url>http://www.example.com/Article.aspx?id=1909</url>
<normalized_url>http://www.example.com/Article.aspx?id=1909</normalized_url>
<share_count>11</share_count>
<like_count>3</like_count>
<comment_count>0</comment_count>
<total_count>14</total_count>
<commentsbox_count>8</commentsbox_count>
<comments_fbid>10150846665210566</comments_fbid>
<click_count>0</click_count>
</link_stat>
</fql_query_response>
<link_stat>
<url>http://www.example.com/Article.aspx?id=1989</url>
<normalized_url>http://www.example.com/Article.aspx?id=1989</normalized_url>
<share_count>11</share_count>
<like_count>3</like_count>
<comment_count>0</comment_count>
<total_count>14</total_count>
<commentsbox_count>8</commentsbox_count>
<comments_fbid>10150846665210566</comments_fbid>
<click_count>0</click_count>
</link_stat>
</fql_query_response>

The thing is i got in the return value: "www.example.com/Article.aspx?id=1132http://www.example.com/Article.aspx?id=190900000101502138970422760" what am i doing wrong? thanks!


回答1:


The problem is you are Selecting a single node but obtaining the content from the root element. On top of that, you have a root namespace, therefore use of NameSpaceManger for searches is required. Try this sample:

        string GuideID = "1989";
        string Url = "'" + "http://www.example.com/Article.aspx?id=" + GuideID + "'";

        var XmlDoc = new XmlDocument();

        XmlDoc.Load(new FileStream("XMLFile1.xml",FileMode.Open,FileAccess.Read));
        var nsm = new XmlNamespaceManager(XmlDoc.NameTable);
        nsm.AddNamespace("s", "http://api.facebook.com/1.0/");

        var node = XmlDoc.SelectSingleNode("s:fql_query_response/s:link_stat[s:normalized_url=" + Url + "]/s:share_count", nsm);
        var ShareCount = node.InnerText;            

And here is the LINQ way with same namespace manager and an XPathSelector.

XDocument xdoc = XDocument.Load(new FileStream("XMLFile1.xml", FileMode.Open, FileAccess.Read));
        var lnode = xdoc.XPathSelectElements("s:fql_query_response/s:link_stat[s:normalized_url=" + Url + "]/s:share_count",nsm).First();
        var ret = lnode.Value;


来源:https://stackoverflow.com/questions/11691527/cant-get-inner-text-from-fql-using-xpath-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!