开发者

Parse XML structure in .net 2

开发者 https://www.devze.com 2023-02-06 10:54 出处:网络
I have the following xml fragment <converters c1=\"XXX\" c2=\"ZZZ\"> <converter c1=\"YYY\" c2=\"ZZZ\"

I have the following xml fragment

<converters c1="XXX" c2="ZZZ">
    <converter c1="YYY" c2="ZZZ" 
               buy="0.9989907开发者_StackOverflow社区0428571424" sell="0.99966215285714288" />
    <converter c1="XXX" c2="YYY" 
               buy="1.5503238471428571" sell="1.550773867142857" />
    <converter c1="XXX" c2="ZZZ" 
               buy="1.5487591119281807" sell="1.5502499426226253" />
</converters>

I am trying to retrieve the value of the number in the "buy" attribute for the converter that has c1="XXX" and c2="ZZZ".

I can't use linq to XML unfortunatley or this would be easy (for me). So I guess I am stuck using xpath

I've created an XPathNavigator but can't get the syntax to get the valu I want

Anyone, any idea how to do this?


XmlDocument doc = new XmlDocument();
                doc.LoadXml("");
                XmlNodeList list = doc.SelectNodes("converters/converter");

                foreach (XmlNode element in list)
                {
                    if (element.Attributes["c1"].Value == "XXX" /*other operations*/) 

                }


If you use XPathDocument you can do

foreach (XPathNavigator buy in new XPathDocument("input.xml").CreateNavigator().Select("converters/converter[@c1 = 'XXX' and @c2 = 'ZZZ']/@buy"))
{
  Console.WriteLine(buy.Value);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号