Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML Azure WebService
Hi,
I am trying to find the balance of the MSFT translator from the azure marketplace b looking at the uri
https://api.datamarket.azure.com/Services/My/Datasets
 
So a code snippet is :
 
Uri targetUri = new Uri("https://api.datamarket.azure.com/Services/My/Datasets");  
NetworkCredential myCredentials = new NetworkCredential(myUserName, myAzureKey);             
WebClient myWebClient = new WebClient();            
myWebClient.Credentials = myCredentials;            
myWebClient.BaseAddress = myUrl;                            
string myResonse = myWebClient.DownloadString(targetUri);                 
XmlDocument myXmlDoc = new XmlDocument();                
myXmlDoc.LoadXml(myResonse);                
// Set up namespace manager for XPath                  
XmlNamespaceManager myXmlNamespaceManager = new XmlNamespaceManager(myXmlDoc.NameTable);                
myXmlNamespaceManager.AddNamespace("base", "https://api.datamarket.azure.com/Services.svc/My/Datasets");                
myXmlNamespaceManager.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");                
myXmlNamespaceManager.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");                
myXmlNamespaceManager.AddNamespace("", "http://www.w3.org/2005/Atom");                 
XmlNode myNode = myXmlDoc.LastChild;                
XmlNodeList myXmlNodeList = myNode.SelectNodes("entry/content/m:properties", myXmlNamespaceManager);  

.....
 
But the nodelist is empty so I cannot get to the the d:ResourceBalance atribute in the list of data services subscribed too.
 
Has anyone suggestions as to how I can walk the DOM to get to this entry - or what Im doing wrong ?
 
Cheers
 
Peter
Posted 9-Apr-12 1:28am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
finally figured it out - but if anyone can provide a more elegant solution please do!
Anyway - here is the code for anyone whose interested:
public static int countRemaining(string myUserName, string myAzureKey, string myAzureBalanceUrl)
        {
            string myNumber = string.Empty;
            int myReturn = 0;
            StreamReader StreamHandler;
            XmlDocument myXmlDoc = new XmlDocument();
            bool myTranslatorFlag = false;
 
            Uri targetUri = new Uri(myAzureBalanceUrl);
            NetworkCredential myCredentials = new NetworkCredential(myUserName, myAzureKey);
 
            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(targetUri);
                myHttpWebRequest.Credentials = myCredentials;
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
 
                if (myHttpWebRequest.HaveResponse)
                {
                    if (myHttpWebResponse.StatusCode == HttpStatusCode.OK)
                    {
                        StreamHandler = new System.IO.StreamReader(myHttpWebResponse.GetResponseStream());
                        myXmlDoc.LoadXml(StreamHandler.ReadToEnd());
 
                        XmlNodeList myParentNodeList = myXmlDoc.ChildNodes;
                        XmlNode myFeed = myParentNodeList[0];
                        XmlNodeList myEntryList = myFeed.ChildNodes;
 
                        foreach (XmlNode myAzureNode in myEntryList)
                        {
                            if (myAzureNode.Name == "entry")
                            {
                                XmlNodeList myContent = myAzureNode.ChildNodes;
                                foreach (XmlNode myEntryNode in myContent)
                                {
                                    if (myEntryNode.Name == "title" && myEntryNode.InnerText == "Microsoft Translator")
                                    {
                                        myTranslatorFlag = true;
                                    }
 
                                    if (myEntryNode.Name == "content" && myTranslatorFlag)
                                    {
                                        XmlNodeList myTemp = myEntryNode.ChildNodes;
                                        XmlNode myTempNode = myTemp[0];
                                        XmlNodeList myProperties = myTempNode.ChildNodes;
 
                                        foreach (XmlNode myValueNode in myProperties)
                                        {
                                            if (myValueNode.Name == "d:ResourceBalance")
                                            {
                                                myNumber = myValueNode.InnerText;
                                            }
                                        }
                                    }
                                }
                            }
                        }
 
                    }
                }
 
            }
            catch (Exception)
            {
                myNumber = "0";
            }
 

            bool myTest = Int32.TryParse(myNumber, out myReturn);
 
            if (!myTest) myReturn = 0;
 
            return myReturn;
        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web01 | 2.8.1411022.1 | Last Updated 11 Apr 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100