【问题标题】:Linq requests in XML fileXML 文件中的 Linq 请求
【发布时间】:2019-04-08 15:00:25
【问题描述】:

这是我的 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()

但不知道如何让它工作...... 感谢您的帮助!

【问题讨论】:

  • 你的xml没有一个根?
  • 我用root编辑过
  • 尝试以下操作: var results = doc.Descendants("line").Where(x => x.Elements("cell").Any(y => (string)y.Attribute(" width") == "79" && (string)y == "CL00295")).ToList();

标签: c# xml wpf linq request


【解决方案1】:

我创建了一个名为“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();

这个答案包含所有的空检查!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多