从后代访问父级属性

时间:2013-11-28 15:07:36

标签: c# xml linq xml-parsing

我有一个像这样的XML文件:

<reservation>
  <day name="monday">
    <slot number="1">
      <name>..</name>
      <description>..</description>
    </slot>
    <slot number="3">
      <name>..</name>
      <description>..</description>
    </slot>
  </day>
  <day name="friday">
    <slot number="4">
      <name>..</name>
      <description>..</description>
    </slot>
  </day>
</reservation>

我需要编写一个LINQ查询,在那里我可以一次访问name天和slot属性和元素。

如何做到这一点?

5 个答案:

答案 0 :(得分:1)

也许可以以更好的方式完成,但是:

string xml = @"<reservation>
    <day name=""monday"">
        <slot number=""1"">
            <name>name</name>
            <description>desc</description>
        </slot>
        <slot number=""3"">
            <name>..</name>
            <description>..</description>
        </slot>
    </day>
    <day name=""friday"">
        <slot number=""4"">
            <name>..</name>
            <description>..</description>
        </slot>
    </day>
    </reservation>";

var element = XElement.Parse(xml);

var res = from day in element.Elements("day")
          from slot in day.Elements()
          from slotName in slot.Elements("name")
          from slotDesc in slot.Elements("description")
          select new
          {
            Day = day.Attribute("name").Value, 
            Slot = slot.Attribute("number").Value,
            Name = slotName.Value,
            Desc = slotDesc.Value
          };

会产生这样的结果:

Day    Slot Name Desc 
monday 1    name desc 
monday 3    ..   .. 
friday 4    ..   .. 

答案 1 :(得分:0)

var xDocument = XDocument.Parse(/* ... */);

var slots =
   xDocument.Descendants("day")
            .SelectMany(day => day.Elements("slot")
                                  .Select(
                                    slot => new
                                      {
                                        DayName = day.Attribute("name").Value,
                                        Name = slot.Element("name").Value,
                                        Number = slot.Attribute("number").Value,
                                        //...
                                      }));

答案 2 :(得分:0)

也许这样的事情适合你:

var slots = xelement.Descendants("slot")
                    .Select(e => new
                    {
                        Slot = e, Day = e.Parent
                    }).ToList();

您可以进一步修改它以仅获取有趣的属性或元素。

答案 3 :(得分:0)

XObject班级有a Parent property,因此对于您可以执行的广告位s

s.Parent.Attribute("name").Value

虽然这有点难以阅读 - 我们怎么知道s.Parent是一个插槽?您只需访问day级别所需的所有信息:

var days = xDocument.Descendants("day");
var allSlots = days.SelectMany(d => {
  // Your code here...
  var daySlots = d.Descendants("slot");
  return daySlots.Select(s => String.Format("{0} {1}",
                              d.Attribute("name").Value,
                              s.Attribute("number").Value));
});

答案 4 :(得分:0)

var document =  XDocument.Load(xml);

var query = from stol in document.Descendants("slot")
             select new
                 {
                     Day = (string) stol.Ancestors().First().Attribute("name"),
                     Number = (int) stol.Attribute("number"),
                     Name = (string) stol.Descendants("name").SingleOrDefault(),
                     Description = (string) stol.Descendants("description").SingleOrDefault(),
                 };