Xamarin.Android How to Get Google Play Store app v

2019-09-03 12:00发布

问题:

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;