Xamarin.Android How to Get Google Play Store app version number using Dcsoup Nuget Plugin?

痴心易碎 提交于 2019-12-13 02:58:01

问题


I am trying to get the latest version number of my store app in order to notify user for updates if they are using an older version.

This is my code so far but its obviously just retrieving the div containing the text "Version Number". How do I get the actual version number (in this case 1.1) referring to the attached screenshot of the DOM tree?

public static string GetAndroidStoreAppVersion()
        {
            string androidStoreAppVersion = null;

            try
            {
                using (var client = new HttpClient())
                {
                    var doc = client.GetAsync("https://play.google.com/store/apps/details?id=" + AppInfo.PackageName + "&hl=en_CA").Result.Parse();
                    var versionElement = doc.Select("div:containsOwn(Current Version)");
                    androidStoreAppVersion = versionElement.Text;
                }
            }
            catch (Exception ex)
            {
                // do something
                Console.WriteLine(ex.Message);
            }

            return androidStoreAppVersion;
        }


回答1:


According to the parser doc,the containsOwm selector selects elements that directly contain the specified text. As a result, your code

var versionElement = doc.Select("div:containsOwn(Current Version)");

will surely return "Current Version". The real element you would like to get is the child of the child of the sibling of "Current Version" element. So you would have to get that element using the selector. So you can get the version number in this way:

            var versionElement = doc.Select("div:containsOwn(Current Version)");
            Element headElement = versionElement[0];
            Elements siblingsOfHead = headElement.SiblingElements;
            Element contentElement = siblingsOfHead.First;
            Elements childrenOfContentElement = contentElement.Children;
            Element childOfContentElement = childrenOfContentElement.First;
            Elements childrenOfChildren = childOfContentElement.Children;
            Element childOfChild = childrenOfChildren.First;
            androidStoreAppVersion = childOfChild.Text;



来源:https://stackoverflow.com/questions/53073942/xamarin-android-how-to-get-google-play-store-app-version-number-using-dcsoup-nug

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