Help with XML  
Author Message
Bigmo





PostPosted: XML and the .NET Framework, Help with XML Top

Sorry I’m new to C#. Trying to retrieve information from the following XML code with the AccountFunds” routine below. There is a class that is drived from the API XSD file, in here “accountSnapshot.availableToBetBalance” is declared as a double.

The code line “accountSnapshot.availableToBetBalance = nodeList.Item(0).InnerXml;” raises a Error “Cannot implicitly convert type 'string' to 'double'”.

Ive tryed this line “accountSnapshot.availableToBetBalance = ((double)(nodeList.Item(0).InnerXml));” but this gives rise to a “Cannot convert type 'string' to 'double'”

How do you convert the nodeList.Item(0).InnerXml” so that it is a double.

Is the AccountFunds” routine the logical way to extract the XML code.

The XML code

< xml version="1.0" encoding="UTF-8" standalone="yes" >

< xml-stylesheet type="text/xsl" href="/rest/apiStylesheet.xsl" >

<accountSnapshot currency="GBP" xmlns="https://api.betfairgames.com/rest/v1">

<exposureLimit>-5000.0</exposureLimit>

<availableToBetBalance>14.87</availableToBetBalance>

<availableToTransferOut>14.8799999999999</availableToTransferOut>

<betfairGamesBalance>25.76</betfairGamesBalance>

<currentExposure>-10.88</currentExposure>

<retainedCommission>0.0</retainedCommission>

</accountSnapshot>

public void AccountFunds(Account account)

{

HttpAccountRequest request = new HttpAccountRequest();

request.Method = "Get";

request.Account = account;

//Execute the request and get the response

HttpAccountResponse response = request.GetResponse();

localresponse = response.ToString();

XmlDocument XMLdoc = new XmlDocument();

XMLdoc.LoadXml(localresponse);

XmlElement RootNode = XMLdoc.DocumentElement;

XmlNodeList nodeList = RootNode.GetElementsByTagName("availableToBetBalance");

accountSnapshot.availableToBetBalance = nodeList.Item(0).InnerXml;

//accountSnapshot.availableToBetBalance = ((double)(nodeList.Item(0).InnerXml));

response.Close();

}



.NET Development8  
 
 
Martin Honnen





PostPosted: XML and the .NET Framework, Help with XML Top

It is not quite clear what you want to achieve. If you have a .NET class declaration matching that XML then you do not need XmlDocument at all, instead you could use XmlSerializer and deserialize the XML markup to a .NET object with typed properties. Show us the class declaration you have if you need help with that approach.

If you want to use XmlDocument however then with .NET 1.x assuming you have a string (localresponse) with the XML you can do it as follows, using XmlConvert.ToDouble:

XmlDocument XMLDoc = new XmlDocument();

XMLDoc.LoadXml(localresponse);

XmlNamespaceManager namespaceManager = new XmlNamespaceManager(XMLDoc.NameTable);

namespaceManager.AddNamespace("bt", "https://api.betfairgames.com/rest/v1");

as XmlElement;

if (availableToBetBalance != null)

{

double balance = XmlConvert.ToDouble(availableToBetBalance.InnerText);

Console.WriteLine("Found balance: {0}.", balance);

}

else

{

Console.WriteLine("No element found.");

}

The same is possible with .NET 2.0 but that also has other methods in XPathNavigator to read out the element content as a double.



 
 
Bigmo





PostPosted: XML and the .NET Framework, Help with XML Top

Thanks for the answer.

How do you refrence the currency element in the line

"<accountSnapshot currency="GBP" xmlns="https://api.betfairgames.com/rest/v1">"


 
 
Martin Honnen





PostPosted: XML and the .NET Framework, Help with XML Top

currency is an attribute not an element, the easiest way in your sample is to simply access

XMLDoc.DocumentElement.GetAttribute("currency")

which returns a string "GBP" in that example.



 
 
Bigmo





PostPosted: XML and the .NET Framework, Help with XML Top

thanks