简体   繁体   中英

is there any function to get numeric , double value from string from xml using linq c#

Have Tried this but need in linq c#

Get value double value from string using RegEx in vb.net

 <Root>
    <Amount>
        <Rate> INR to USD  3.0245</Rate>
    </Amount>
    <Amount>
        <Rate> Dong to INR  5.201454</Rate>
    </Amount>
    </Root>

 string xml = @"<Root>
                        <Amount>
                            <Rate> INR to USD  3.0245</Rate>
                        </Amount>
                        <Amount>
                            <Rate> Dong to INR  5.201454</Rate>
                        </Amount>
                        </Root>";

            XDocument Doc = XDocument.Parse(xml);

            var list = Doc.Descendants("Amount").Select(y => new
            {
                Rate = (string)y.Element("Rate")
            }).FirstOrDefault();

need Output : 3.0245, 5.201454 using linq c#

From your output it seems all you need is the numbers themselves so I look for the <Rate> descendants. Then for each of them use regex (regex was adapted to case out of this question: Extract decimal from string ) to find the number and return it:

string xml = @"<Root>
               <Amount>
                   <Rate> INR to USD  3.0245</Rate>
               </Amount>
               <Amount>
                   <Rate> Dong to INR  5.201454</Rate>
               </Amount>
               </Root>";

XDocument Doc = XDocument.Parse(xml);
var list = Doc.Descendants("Rate")
              .Select(y => Convert.ToDouble(Regex.Match(y.Value, @"\d+(?:\.\d+)?").Value))
              .ToList();

In the case that you have <Rate> elements with no numbers or if a single one might have more than 1 number use:

 var list = Doc.Descendants("Rate")
               .SelectMany(y => Regex.Matches(y.Value, @"\d+(?:\.\d+)?").Cast<Match>().Select(x => x.Value))
               .Select(y => Convert.ToDouble(y))
               .ToList();

You need a regex for parsing the string:

XDocument Doc = XDocument.Parse(xml);

var r = new Regex(@".*(\d+[\.,]?\d+)");
var list = Doc.Descendants("Amount").Select(y => new
{
    Rate = r.Matches[0]((string)y.Element("Rate").Value)
});

var rate = Convert.ToDouble(list.First().Rate);

The regex will match both notations using a colon and a dot as decimal-seperator. When you need only the dot the regx is simplified to @".*(\\d+[\\.]?\\d+)" .

If you want to parse it without using a regex, you could try this as well. This code relies on the fact that the numerical rate value is at the end of the string and has a space ' ' in front of it.
So the text after the last space is parsed as a double . The CultureInfo is set to en to ensure that the decimal point is recognized correctly.

var doc = XDocument.Parse(input);
double[] values = doc.Root
    .Descendants("Rate")
    .Select( elem => double.Parse(elem.Value.Substring(elem.Value.LastIndexOf(' ')),CultureInfo.GetCultureInfoByIetfLanguageTag("en")))
    .ToArray ();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM