【问题标题】:Reading Sub-Elements (child nodes) with XMLreader using C#使用 C# 使用 XMLreader 读取子元素(子节点)
【发布时间】:2016-01-03 21:16:46
【问题描述】:

首先:这不是Reading Child Nodes with XMLReader 的重复项 (语言不同,无法帮助我)。

我对 XMLreading 很陌生,我正在尝试访问特定元素的子元素,但我很难达到它,这是一个示例:

XML 元素:

<Group ExerciseNumber="0" Name="R33-IOS1_IOS1" ExerciseName="Unallocated" Status="offline" StatusStatistics="0/1">
      <Clients>
        <Client ClientName="R33-IOS1_IOS1" MachineName="R33-IOS1" ClientType="HC0" ClientStatus="Disconnected" />
      </Clients>
      <GroupAppendedData GroupID="201" Type="IOS" DomeType="None" ConnectedTo="" ForceType="Enemy" />
    </Group>

我正在尝试从特定的“Group”元素访问“Client”元素,这是我的 C# 代码:

while (reader.Read())
            {
                if (reader.Name.Equals("Group"))
                {
                    name = reader.GetAttribute("Name");
                    // Now I need to reach the specific "MachineName" attribute of the "Client" sub-element but don't know how.
                }
            }
            reader.Close();

注意事项: 重要的是客户端元素读取将在同一个循环迭代中(如果可能,如果没有,我将不得不为我的生成类考虑另一种设计)。

*编辑 XML 不是一个选项。

谢谢。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    LINQ to XML 比 XmlReader 更容易使用,这将为您提供文档中所有客户端的机器名称:

    var machineNames = XElement.Parse("data.xml")
                               .Descendants("Client")
                               .Select(client => client.Attribute("MachineName").Value);
    

    编辑 - 这会在每次迭代中返回两个名称:

    var query = XElement.Load("data.xml")
                        .Descendants("Client")
                        .Select(client => new {
                                   MachineName = client.Attribute("MachineName").Value,
                                   GroupName = client.Ancestors("Group").Select(g => g.Attribute("Name").Value).First()
                               });
    
    foreach (var x in query)
        Console.WriteLine($"GroupName: {x.GroupName}, MachineName: {x.MachineName}");
    

    【讨论】:

    • 我需要同时访问 Group 的“Name”属性和 Client 元素的“MachineName”属性,我将同时使用它们(作为同一对象的字段),所以这很重要我将在同一次迭代中收集这两个名称。
    【解决方案2】:

    按照建议,除非您有充分的理由使用 XmlReader,否则最好使用更高级别的 API,例如 LINQ to XML。

    这是一个使用查询语法的解决方案:

    var clients = from @group in doc.Descendants("Group")
                  let groupName = (string) @group.Attribute("Name")
                  from client in @group.Descendants("Client")
                  select new
                  {
                      GroupName = groupName,
                      ClientName = (string) client.Attribute("ClientName"),
                      MachineName = (string) client.Attribute("MachineName"),
                  };
    

    有关工作示例,请参阅 this fiddle

    【讨论】:

      【解决方案3】:

      在 xml linq 中尝试 ReadFrom() 方法

                  while (reader.Read())
                  {
                      if (reader.Name.Equals("Group"))
                      {
                          XElement group =  (XElement)XDocument.ReadFrom(reader);
                      }
                  }
                  reader.Close();​
      

      【讨论】:

        猜你喜欢
        • 2019-03-08
        • 1970-01-01
        • 2014-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多