Получить значения из XML-документа с помощью linqToXml - PullRequest
0 голосов
/ 23 марта 2011

Я просто не могу понять это правильно.Я пытался осмотреться, но мои запросы не дали результатов.

Это XML:

<?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>

И мой метод (не сделано ofc)

private static Dictionary<string, int> GetPrizesFromXML()
{
    //Read the values from the XMLDoc
    XElement xml = XElement.Load("prices.xml");
    var prizes = from q in xml.Elements("Prices") 
                 select q.Elements("Item");
    foreach (var prize in prizes)
    {
        Console.Out.WriteLine(prize.ToString());
    }

    return null;

}

1 Ответ

3 голосов
/ 23 марта 2011

Изменить эту строку:

var prizes = from q in xml.Elements("Prices") 
             select q.Elements("Item");

к этому:

var prizes = from q in xml.Elements("Prices") 
             select q.Descendants("Item");
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...