【发布时间】:2020-09-01 07:31:09
【问题描述】:
我对 C# 很陌生。我有 XML 文件。我想遍历所有节点并逐节点显示值。最后,我想将此值存储在字符串中。例如,我想要这样的东西:
第一次迭代:
- 字符串名称:比利时华夫饼
- 字符串价格:5.95 美元
- 字符串描述:我们著名的两个比利时华夫饼,加入大量真正的枫糖浆
- 串卡路里:650
第二次迭代:
- 字符串名称:草莓比利时华夫饼
- 字符串价格:7.95 美元
- 字符串描述:轻薄的比利时华夫饼,上面覆盖着草莓和生奶油
- 串卡路里:900
等等
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description>
Two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>
Light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>
Belgian waffles covered with assorted fresh berries and whipped cream
</description>
<calories>900</calories>
</food>
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>
Thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
<description>
Two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
【问题讨论】:
-
也许您可以先阅读一下这是如何完成的。例如:stackoverflow.com/questions/642293/…。如果有具体问题再回来。
-
我建议您先将 XML 反序列化 到 C# 类中。当适当的数字类型可用时,将数值建模为字符串通常是不好的做法。