[英]Parsing an XML in C#
I have this xml http://www.bnr.ro/nbrfxrates.xml , and I want to parse it. 我有这个xml http://www.bnr.ro/nbrfxrates.xml ,我想解析它。 I have transformed it into a string, but I don't know how to iterate through the Cube's children to get the data I'm interested in (the currency rates).
我已经将其转换为字符串,但是我不知道如何遍历多维数据集的子项来获取我感兴趣的数据(汇率)。 Any help, please?
有什么帮助吗? Thanks.
谢谢。
You can try HtmlAgilityPack . 您可以尝试HtmlAgilityPack 。
And by the help of XPath , all nodes become easily accessible. 借助XPath ,所有节点都变得易于访问。
Code sample: 代码示例:
WebRequest webRequestSearch = WebRequest.Create("http://www.bnr.ro/nbrfxrates.xml");
WebResponse webResponseSearch = webRequestSearch.GetResponse();
Stream streamSearch = webResponseSearch.GetResponseStream();
StreamReader oReaderSearch = new StreamReader(streamSearch, Encoding.GetEncoding(1254));
strTempHtml = oReaderSearch.ReadToEnd();
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(strTempHtml);
HtmlNodeCollection nodes = htmlDoc.DocumentNode.SelectNodes("//cube/rate");
string currency1 = nodes[0].Attributes["currency"].Value;
string currency1Value =nodes[0].InnerText;
You can use LINQ to XML, there is many examples and explanations on this website: 您可以使用LINQ to XML,此网站上有很多示例和说明:
http://www.dotnetcurry.com/showarticle.aspx?ID=564 http://www.dotnetcurry.com/showarticle.aspx?ID=564
XNamespace ns = "http://www.bnr.ro/xsd";
var rates = XDocument.Load("http://www.bnr.ro/nbrfxrates.xml")
.Descendants(ns + "Rate")
.Select(r => new
{
Currency = r.Attribute("currency").Value,
Value = (decimal)r,
Multiplier = (int?)r.Attribute("multiplier")
})
.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.