Best way to read string xml? [Solved]
-
Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...
33True
I want the value "33", have the following and it works fine but wanted to know it's the right way to go...
string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
foreach (char cString in sReturnString.ToCharArray())
{
if (char.IsDigit(cString))
sCredits += cString.ToString();
}
return "Balance: sCredits";I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..
I remain joe!
-
Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...
33True
I want the value "33", have the following and it works fine but wanted to know it's the right way to go...
string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
foreach (char cString in sReturnString.ToCharArray())
{
if (char.IsDigit(cString))
sCredits += cString.ToString();
}
return "Balance: sCredits";I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..
I remain joe!
-
If you don't already have one, add a reference to
System.Xml.Linq
; -
Add
using System.Linq;
andusing System.Xml.Linq;
at the top of your code file; -
Use:
XElement el = XElement.Parse(sReturnString);
string sCredits = (string)el.Elements("data").Elements("credits").FirstOrDefault();
return "Balance: " + sCredits;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
-
-
If you don't already have one, add a reference to
System.Xml.Linq
; -
Add
using System.Linq;
andusing System.Xml.Linq;
at the top of your code file; -
Use:
XElement el = XElement.Parse(sReturnString);
string sCredits = (string)el.Elements("data").Elements("credits").FirstOrDefault();
return "Balance: " + sCredits;
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
-
Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...
33True
I want the value "33", have the following and it works fine but wanted to know it's the right way to go...
string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
foreach (char cString in sReturnString.ToCharArray())
{
if (char.IsDigit(cString))
sCredits += cString.ToString();
}
return "Balance: sCredits";I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..
I remain joe!
I'd use an XmlDocument.
string s = "<api_result><data><credits>33</credits></data><call_result><result>True</result><error /></call_result></api_result>" ;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.LoadXml ( s ) ;System.Xml.XmlNode nod = doc.DocumentElement.SelectSingleNode ( "data/credits" ) ;
string v = nod.InnerText ;
-
Fisrt let me say I dont have errors and all is working fine, I want to know the best way of doing this... I am consumming a web serving that return string xml...
33True
I want the value "33", have the following and it works fine but wanted to know it's the right way to go...
string sReturnString = WebserviceName.ReturnMyValue(PassingSomeValues);
foreach (char cString in sReturnString.ToCharArray())
{
if (char.IsDigit(cString))
sCredits += cString.ToString();
}
return "Balance: sCredits";I don't want to save it to a file, but just want to dislapy the value. Any recommnedations? Thanking you in advance. Addition: .. Saving it to an xml file makes it easy to read the creadit element value 33; but I dont want to create any files..
I remain joe!
Boipelo wrote:
I want to know the best way of doing this...
Depends on what "best" means. But since you are doing nothing but a simple value lookup what you are doing is probably the most performant (although probably not in a significant way.) There are upside/downsides to ignoring the XML itself. For example if they change element names on the message your code still works. But if they add another element first with a numeric value it doesn't. Conversely using XML if they change names then the code would fail. But if they add another element it would still work.
-
Boipelo wrote:
I want to know the best way of doing this...
Depends on what "best" means. But since you are doing nothing but a simple value lookup what you are doing is probably the most performant (although probably not in a significant way.) There are upside/downsides to ignoring the XML itself. For example if they change element names on the message your code still works. But if they add another element first with a numeric value it doesn't. Conversely using XML if they change names then the code would fail. But if they add another element it would still work.
-
I'd use an XmlDocument.
string s = "<api_result><data><credits>33</credits></data><call_result><result>True</result><error /></call_result></api_result>" ;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument() ;
doc.LoadXml ( s ) ;System.Xml.XmlNode nod = doc.DocumentElement.SelectSingleNode ( "data/credits" ) ;
string v = nod.InnerText ;
-
In my opinion this (or using XmlReader directly) is the way to go: Consistency - the web service uses xml responses because it is a standard with some pros e.g. extensibility and formatting. By not parsing xml according to the standard, you don't get the benefits from the standard. Performance - XmlElement.parse is built upon XmlReader which is a sequential reader. It is both faster and cheaper in memory than XmlDocument that builds a complete DOM in memory. Kind Regards, Keld Ølykke
-
In my opinion this (or using XmlReader directly) is the way to go: Consistency - the web service uses xml responses because it is a standard with some pros e.g. extensibility and formatting. By not parsing xml according to the standard, you don't get the benefits from the standard. Performance - XmlElement.parse is built upon XmlReader which is a sequential reader. It is both faster and cheaper in memory than XmlDocument that builds a complete DOM in memory. Kind Regards, Keld Ølykke
-
I was also scared of "...if they add another element with a numeric value". I am not seeing them changing the "element name", that wont happen. Thanks for the comment.
I remain joe!