use*_*131 1 c# xml linq-to-sql
XML:
<shift_details>
    <shift_time>10:00 to 10:30</shift_time>
    <count>0</count>
    <shift_time>10:30 to 11:00</shift_time>
    <count>0</count>
    <shift_time>11:00 to 11:30</shift_time>
    <count>0</count>
    <shift_time>11:30 to 12:00</shift_time>
    <count>0</count>
    <shift_time>12:00 to 12:30</shift_time>
    <count>0</count>
</shift_details>
码:
var slots = from c in xmlResponse.Descendants("shift_details")
            select new TimeSlots
            {
                time = (string)c.Element("shift_time"),
                count = (string)c.Element("count"),
            };
上面的代码只返回一个插槽项作为输出.但我的xml包含太多记录.
如何读取上面xml中的所有记录?
那是因为Element只返回给定名称的第一个元素.您应该考虑更改XML结构以将不同的插槽彼此分开,例如:
<shift_details>
    <shift>
        <shift_time>10:00 to 10:30</shift_time>
        <count>0</count>
    </shift>
    (...)
</shift_details>
然后查询它:
var slots = from c in xmlResponse.Element("shift_details").Elements("shift")
            select new TimeSlots
            {
                time = (string)c.Element("shift_time"),
                count = (string)c.Element("count"),
            };
或者,如果您无法更改XML,您仍然可以查询它,但它会更棘手:
var doc = XDocument.Load("Input.txt");
var details = doc.Root;
var times = details.Elements("shift_time");
var counts = details.Elements("count");
var slots = times.Zip(counts, (t, c) => new { time = (string)t, count = (string)c }).ToList();
| 归档时间: | 
 | 
| 查看次数: | 94 次 | 
| 最近记录: |