Linq-запросы в XML-файле - PullRequest
0 голосов
/ 08 апреля 2019

Это мой XML-файл:

<lines>
<line>
    <cell width="96" align="left" styleclass="5">Madame</cell>
    <cell width="129" align="left" styleclass="5">NATHALIE</cell>
    <cell width="187" align="left" styleclass="5">REGINENSI</cell>
    <cell width="296" align="left" styleclass="5">production@holyfruits.com</cell>
    <cell width="79" align="left" styleclass="5">CL00295</cell>
</line>
<line>
    <cell width="96" align="left" styleclass="5">Madame</cell>
    <cell width="129" align="left" styleclass="5">NICOLE</cell>
    <cell width="187" align="left" styleclass="5">BAROIN</cell>
    <cell width="296" align="left" styleclass="5">nbaroin@skendy-paris.com</cell>
    <cell width="79" align="left" styleclass="5">CL00022</cell>
</line>
</lines>

Я пытаюсь получить все значения ячеек строки, где строка width="79" == CL00295, но я изо всех сил пытаюсь найти правильный синтаксис для запроса linq. Вот что я сделал, что не работает:

var results = from sheet in doc.Descendants("line")
                          where sheet.Descendants("cell").ToString().ToLower() == listeClients[comboBoxClients.SelectedIndex].Id.ToLower()
                          select new
                          {
                              Value = sheet.Descendants("cell")
                                    .Where(t => t.Attribute("width")
                                    .Value == "96") // Civ
                                    .First().Value,
                              Value2 = sheet.Descendants("cell")
                                    .Where(t => t.Attribute("width")
                                    .Value == "129") // Prenom
                                    .First().Value,
                              Value3 = sheet.Descendants("cell")
                                    .Where(t => t.Attribute("width")
                                    .Value == "187") // Nom
                                    .First().Value,
                              Value4 = sheet.Descendants("cell")
                                    .Where(t => t.Attribute("width")
                                    .Value == "296") // Email
                                    .First().Value,
                              Value5 = sheet.Descendants("cell")
                                    .Where(t => t.Attribute("width")
                                    .Value == "79") // Code
                                    .First().Value
                          }.ToString();

Полагаю, неправильная часть моего кода

where sheet.Descendants("cell").ToString().ToLower() == listeClients[comboBoxClients.SelectedIndex].Id.ToLower()

Но не мог понять, как заставить это работать ... Спасибо за вашу помощь!

1 Ответ

1 голос
/ 08 апреля 2019

Я создал файл с именем "xml.xml":

<lines>
    <line>
        <cell width="96" align="left" styleclass="5">Madame</cell>
        <cell width="129" align="left" styleclass="5">NATHALIE</cell>
        <cell width="187" align="left" styleclass="5">REGINENSI</cell>
        <cell width="296" align="left" styleclass="5">production@holyfruits.com</cell>
        <cell width="79" align="left" styleclass="5">CL00295</cell>
    </line>
    <line>
        <cell width="96" align="left" styleclass="5">Madame</cell>
        <cell width="129" align="left" styleclass="5">NICOLE</cell>
        <cell width="187" align="left" styleclass="5">BAROIN</cell>
        <cell width="296" align="left" styleclass="5">nbaroin@skendy-paris.com</cell>
        <cell width="79" align="left" styleclass="5">CL00022</cell>
    </line>
</lines>

Затем я извлек строки с атрибутом с именем width со значением 79, со значением xelement CL00295, после чего я извлекаю ячейки соответствующих строк.

XDocument doc = XDocument.Load("xml.xml");
List<XElement> cells = doc.Descendants("line").Where(z => z.Descendants("cell").Any(x => x.Attributes().FirstOrDefault(y => y.Name == "width")?.Value == "79" && x.Value == "CL00295"))?.Elements("cell").ToList();

Этот ответ содержит все нулевые проверки!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...