【发布时间】: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();