从XML读取帮助想要逐个显示它们而不是全部在同一行上 [英] Read from XML help want to display them one by one not all on same line

查看:128
本文介绍了从XML读取帮助想要逐个显示它们而不是全部在同一行上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试显示来自此xml文件的数据:



Hi,

I am trying to display data from this xml file:

<?xml version="1.0" encoding="utf-8" ?>
<Prices>
  <PricePerItem>
    <Item Name="Milk, Low fat, 1Liter">11.2</Item>
    <Item Name="Butter">17</Item>
    <Item Name="Bread">12.2</Item>
    <Item Name="Cheese">15.5</Item>
  </PricePerItem>
  <PricePerKg>
    <Item Name="Apple, Jonagold">13.4</Item>
    <Item Name="Chicken">12.5</Item>
    <Item Name="Salad">9.6</Item>
    <Item Name="Fish, Salmon">14</Item>
  </PricePerKg>
</Prices>





我试图从每个值中获取值。

我有一个列表框和一个按钮

现在它显示如下

牛奶,低脂肪,1升11.21713.215.5

但我希望它显示11.2 for Milk,因为这是价值而不是来自PricePerItem的所有价值



这是我的按钮代码



I am trying to get out the values from each one.
I have a listbox and a button
Right now it displays like this
Milk,Low Fat, 1 Liter 11.21713.215.5
But i want it to display 11.2 for Milk because thats the value and not all the values from PricePerItem

this is my code for the button

listBox1.Items.Clear();

            var xmlDoc = new XmlDocument();
            xmlDoc.Load(@"c:\users\morski\documents\visual studio 2013\Projects\Test\Test\prices.xml");

            XmlNodeList xnListPerItem = xmlDoc.SelectNodes("/Prices");


                foreach (XmlNode xn in xnListPerItem)
                {

                    string pricePerItem = xn["PricePerItem"].InnerText;

                    
                    //Adds pricePerItem
                    var pricePerItemList= new List<string>();

                    pricePerItemList.Add("Milk, Low fat, 1Liter" + pricePerItem);
                    pricePerItemList.Add("Butter" + pricePerItem);
                    pricePerItemList.Add("Bread" + pricePerItem);

                    foreach (var vItem in pricePerItemList)
                    {
                        
                        listBox1.Items.Add(vItem);
                        
                    }

推荐答案

我怀疑问题的很大一部分是 xmlDoc .SelectNodes(/ Price)

为什么不是 xmlDoc.SelectNodes(/ Price / PricePerItem / Item)



xn [PricePerItem]。InnerText 显然是错误的。



您不应该将物品硬编码到pricePerItemList中,无论是什么。



看看这个:



I suspect that a big part of the problem is xmlDoc.SelectNodes("/Prices") .
Why isn't it xmlDoc.SelectNodes("/Prices/PricePerItem/Item") ?

xn["PricePerItem"].InnerText is obviously wrong.

And you shouldn't be hard-coding items into pricePerItemList, whatever that is.

Have a look at this:

System.Xml.XmlNodeList xnListPerItem = doc.SelectNodes("/Prices/PricePerItem/Item");

var pricePerItemList= new System.Collections.Generic.List<string>();

foreach (System.Xml.XmlNode xn in xnListPerItem)
{
    string temp = System.String.Format ( "{0} {1}" , xn.Attributes [ "Name" ].Value , xn.InnerText ) ;
    pricePerItemList.Add(temp);
}


这篇关于从XML读取帮助想要逐个显示它们而不是全部在同一行上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆