Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse single XML statement in string

Tags:

c#

xml

I ahve experience serializing/deserializing XML files but I have never had to parse just a single statement, so I'm not sure how to go about this.

I have a string that holds this:

<Vol Model_Type="Flat">102.14</Vol>

And, I want to extract just the 102.14.

Should I use XPath, or is there a simpler option?

like image 795
xbonez Avatar asked Nov 19 '25 00:11

xbonez


1 Answers

If you're using .NET 3.5 or above, use LINQ to XML. For example:

string x = "<Vol Model_Type=\"Flat\">102.14</Vol>";
XElement element = XElement.Parse(x);
decimal value = (decimal) element;

XML handling doesn't get much simpler than that :)

Of course, that's assuming you don't care about the element name or the attribute. If you do, LINQ to XML will still make it easy for you.

like image 107
Jon Skeet Avatar answered Nov 20 '25 13:11

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!