【问题标题】:How to read this little piece of XML?如何阅读这一小段 XML?
【发布时间】:2013-03-29 11:09:14
【问题描述】:

我一直卡在这个 XML 文件上。我的 XAML 中有 12 个 TextBlock,我想将每个节点放在相应的 TextBlock 中

<Producten>
  <Tariefeenheden>52</Tariefeenheden>
  <Product naam="Enkele reis">
     <Prijs korting="vol tarief" klasse="2">9.00</Prijs>
     <Prijs korting="reductie_20" klasse="2">7.20</Prijs>
     <Prijs korting="reductie_40" klasse="2">5.40</Prijs>
     <Prijs korting="vol tarief" klasse="1">15.30</Prijs>
     <Prijs korting="reductie_20" klasse="1">12.20</Prijs>
     <Prijs korting="reductie_40" klasse="1">9.20</Prijs>
  </Product>
  <Product naam="Dagretour">
     <Prijs korting="vol tarief" klasse="2">18.00</Prijs>
     <Prijs korting="reductie_20" klasse="2">14.40</Prijs>
     <Prijs korting="reductie_40" klasse="2">10.80</Prijs>
     <Prijs korting="vol tarief" klasse="1">30.60</Prijs>
     <Prijs korting="reductie_20" klasse="1">24.40</Prijs>
     <Prijs korting="reductie_40" klasse="1">18.40</Prijs>
  </Product>
</Producten>

我怎样才能做到这一点?

亲切的问候, 尼尔斯

编辑:我想获得 .. 的值,例如“9.00”、“7,20”等。

【问题讨论】:

  • 把每个节点放到对应的TextBlock中你有多少个文本框?如果每个节点都有一个文本框,您想获取什么信息(Korting 或 Klasse 或 prijs 值)?
  • 我想获得价值(所以金额)。 9.00、7.20、5.40 等数字。

标签: c# xml windows-phone-7


【解决方案1】:

这样可以读取klasse的所有字符串值

XmlDocument xml = new XmlDocument();
xml.Load(YOUR XML FILE PATH);

XmlNodeList xnList = xml.SelectNodes("Producten/Product/Prijs");
List<string> values = new List<string>();

foreach (XmlNode xn in xnList)
{
    var result = xn.Attributes["klasse"].InnerText;
}

//textBlock1.Text = values[0];
//textBlock2.Text = values[1];
//textBlock3.Text = values[2];

编辑:如果您在 WindowPhone 中执行此操作,您可以使用XDocument。代码如下:

var doc = System.Xml.Linq.XDocument.Load(YOUR XML FILE PATH);
List<string> values = new List<string>();
foreach (System.Xml.Linq.XElement item in doc.Descendants("Prijs"))
{
     values.Add(item.Attribute("klasse").Value);
}

//textBlock1.Text = values[0];
//textBlock2.Text = values[1];
//textBlock3.Text = values[2];

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-14
  • 2022-08-17
  • 1970-01-01
  • 2014-02-20
  • 2010-12-02
  • 2023-03-26
  • 1970-01-01
相关资源
最近更新 更多