如果是嵌套元素,则后代 Until()

2023-12-27

在我之前的问题中:link https://stackoverflow.com/questions/75957548/xdocument-descendants-cannot-distinguish-between-parent-child-elements,

我学习了如何使用名为“后代直到()”的扩展方法来查找最顶层元素(父元素)及其最顶层后代元素(子元素)。

现在让我们假设有更多的后代(父母、孩子、侄子等..):

<?xml version="1.0" encoding="utf-8"?>
<Document>
    <Interface>
        <Sections xmlns="http://www.siemens.com/automation/Openness/SW/Interface/v4">
            <Section Name="Static">
                <Member Name="3bool1" Datatype="&quot;3bool&quot;" Remanence="NonRetain" Accessibility="Public">
                    <AttributeList>
                        <BooleanAttribute Name="ExternalAccessible" SystemDefined="true">true</BooleanAttribute>
                        <BooleanAttribute Name="ExternalVisible" SystemDefined="true">true</BooleanAttribute>
                        <BooleanAttribute Name="ExternalWritable" SystemDefined="true">true</BooleanAttribute>
                        <BooleanAttribute Name="SetPoint" SystemDefined="true">false</BooleanAttribute>
                    </AttributeList>
                    <Sections>
                        <Section Name="None">
                            <Member Name="bool1" Datatype="Bool" />
                            <Member Name="bool2" Datatype="Bool" />
                            <Member Name="bool3" Datatype="Bool" />
                            <Member Name="3bool1" Datatype="&quot;3bool&quot;" Remanence="NonRetain" Accessibility="Public">
                                <AttributeList>
                                    <BooleanAttribute Name="ExternalAccessible" SystemDefined="true">true</BooleanAttribute>
                                    <BooleanAttribute Name="ExternalVisible" SystemDefined="true">true</BooleanAttribute>
                                    <BooleanAttribute Name="ExternalWritable" SystemDefined="true">true</BooleanAttribute>
                                    <BooleanAttribute Name="SetPoint" SystemDefined="true">false</BooleanAttribute>
                                </AttributeList>
                                <Sections>
                                    <Section Name="None">
                                        <Member Name="bool1" Datatype="Bool" />
                                        <Member Name="bool2" Datatype="Bool" />
                                        <Member Name="bool3" Datatype="Bool" />
                                    </Section>
                                </Sections>
                            </Member>
                        </Section>
                    </Sections>
                </Member>
                <Member Name="int7" Datatype="Int" Remanence="NonRetain" Accessibility="Public">
                    <AttributeList>
                        <BooleanAttribute Name="ExternalAccessible" SystemDefined="true">true</BooleanAttribute>
                        <BooleanAttribute Name="ExternalVisible" SystemDefined="true">true</BooleanAttribute>
                        <BooleanAttribute Name="ExternalWritable" SystemDefined="true">true</BooleanAttribute>
                        <BooleanAttribute Name="SetPoint" SystemDefined="true">true</BooleanAttribute>
                    </AttributeList>
                </Member>
            </Section>
        </Sections>
    </Interface>
 </Document>

通过使用 DescendantsUntil() 扩展方法,我可以轻松过滤父级和子级

string path = ("C:\\Users\\");
XDocument doc = XDocument.Load(path + "File.xml");
XNamespace ns = "http://www.siemens.com/automation/Openness/SW/Interface/v4";
XName name = ns + "Member";

var memb = doc
    .Root.DescendantsUntil(e => e.Name == name)
    .Select(e => (Parent: e, Children: e.DescendantsUntil(c => c.Name == name).ToList()))
    //.Where(i => i.Children.Count > 0); // Uncomment to filter out <Member> elements with no child members.
    .ToList();
  

现在,我如何使用 DescendantsUntil() 来提取 Parent、Children、Nephews 元素,以及一般来说,只要存在另一个嵌套元素,如何提取所有后代?


尝试这样的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication52
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement xSections = doc.Descendants().Where(x => x.Name.LocalName == "Sections").FirstOrDefault();
            XNamespace ns = xSections.GetDefaultNamespace();
            Sections root = new Sections();
            root.ParseSections(xSections, ns);
        }
    }
    public class Sections
    {
        public List<Section> sections { get; set; }

        public void ParseSections(XElement xSections, XNamespace ns)
        {
            List<XElement> sections = xSections.Elements(ns + "Section").ToList();
            foreach (XElement section in sections)
            {
                if (this.sections == null) this.sections = new List<Section>();
                Section childSection = new Section();
                this.sections.Add(childSection);
                childSection.ParseSection(section, ns);
            }
        }
    }
    public class Section
    {
        public string name { get; set; }
        public List<Member> members { get; set; }

        public void ParseSection(XElement xSection, XNamespace ns)
        {
            this.name = (string)xSection.Attribute("Name");
            foreach (XElement xMember in xSection.Elements(ns + "Member"))
            {
                if (this.members == null) this.members = new List<Member>();
                Member member = new Member();
                this.members.Add(member);
                member.ParseMember(xMember, ns);

            }
        }
    }
    public class Member
    {
        public string name { get; set; }
        public string remanence { get; set; }
        public string accessibility { get; set; }
        public Dictionary<string, Boolean> attributes { get; set; }
        public Sections sections { get; set; }

        public void ParseMember(XElement member, XNamespace ns)
        {
            this.name = (string)member.Attribute("Name");
            this.remanence = (string)member.Attribute("Remanence");
            this.accessibility = (string)member.Attribute("Accessibility");
            XElement attributeList = member.Element(ns + "AttributeList");
            if (attributeList != null)
            {
                foreach (XElement attribute in attributeList.Descendants(ns + "BooleanAttribute"))
                {
                    if (attributes == null) attributes = new Dictionary<string, bool>();
                    string attributeName = (string)attribute.Attribute("Name");
                    Boolean attributeValue = (Boolean)attribute;
                    attributes.Add(attributeName, attributeValue);
                }
            }
            XElement xSections = member.Element(ns + "Sections");
            if (xSections != null)
            {
                Sections childSections = new Sections();
                this.sections = childSections;
                childSections.ParseSections(xSections, ns);
            }
            
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果是嵌套元素,则后代 Until() 的相关文章

  • 使用 Unity 在构造函数中使用属性依赖注入

    好的 我在基类中定义了一个依赖属性 我尝试在其派生类的构造函数内部使用它 但这不起作用 该属性显示为 null Unity 在使用 container Resolve 解析实例后解析依赖属性 我的另一种选择是将 IUnityContaine
  • Unix网络编程澄清

    我正在翻阅这本经典书籍Unix网络编程 https rads stackoverflow com amzn click com 0139498761 当我偶然发现这个程序时 第 6 8 节 第 179 180 页 include unp h
  • 在新的浏览器进程中打开 URL

    我需要在新的浏览器进程中打开 URL 当浏览器进程退出时我需要收到通知 我当前使用的代码如下 Process browser new Process browser EnableRaisingEvents true browser Star
  • 单元测试一起运行时失败,单独运行时通过

    所以我的单元测试遇到了一些问题 我不能只是将它们复制并粘贴到这里 但我会尽力而为 问题似乎是 如果我一项一项地运行测试 一切都会按预期进行 但如果我告诉它一起运行测试 则 1 5 将通过 TestMethod public void Obj
  • C++中的类查找结构体数组

    我正在尝试创建一个结构数组 它将输入字符串链接到类 如下所示 struct string command CommandPath cPath cPathLookup set an alarm AlarmCommandPath send an
  • 使用 C 语言使用 strftime() 获取缩写时区

    我看过this https stackoverflow com questions 34408909 how to get abbreviated timezone and this https stackoverflow com ques
  • 关于在 Windows 上使用 WiFi Direct Api?

    我目前正在开发一个应用程序 我需要在其中创建链接 阅读 无线网络连接 在桌面应用程序 在 Windows 10 上 和平板电脑 Android 但无关紧要 之间 工作流程 按钮 gt 如果需要提升权限 gt 创建类似托管网络的 WiFi 网
  • Rx 中是否有与 Task.ContinueWith 运算符等效的操作?

    Rx 中是否有与 Task ContinueWith 运算符等效的操作 我正在将 Rx 与 Silverlight 一起使用 我正在使用 FromAsyncPattern 方法进行两个 Web 服务调用 并且我想这样做同步地 var o1
  • 未定义的行为或误报

    我 基本上 在野外遇到过以下情况 x x 5 显然 它可以在早期版本的 gcc 下编译干净 在 gcc 4 5 1 下生成警告 据我所知 警告是由 Wsequence point 生成的 所以我的问题是 这是否违反了标准中关于在序列点之间操
  • 未经许可更改内存值

    我有一个二维数组 当我第一次打印数组的数据时 日期打印正确 但其他时候 array last i 的数据从 i 0 到 last 1 显然是一个逻辑错误 但我不明白原因 因为我复制并粘贴了 for 语句 那么 C 更改数据吗 I use g
  • 在一个字节中存储 4 个不同的值

    我有一个任务要做 但我不知道从哪里开始 我不期待也绝对不想要代码中的答案 我想要一些关于该怎么做的指导 因为我感到有点失落 将变量打包和解包到一个字节中 您需要在一个字节中存储 4 个不同的值 这些值为 NAME RANGE BITS en
  • 如何将整数转换为 void 指针?

    在 C 中使用线程时 我面临警告 警告 从不同大小的整数转换为指针 代码如下 include
  • C++:.bmp 到文件中的字节数组

    是的 我已经解决了与此相关的其他问题 但我发现它们没有太大帮助 他们提供了一些帮助 但我仍然有点困惑 所以这是我需要做的 我们有一个 132x65 的屏幕 我有一个 132x65 的 bmp 我想遍历 bmp 并将其分成小的 1x8 列以获
  • 批量更新 SQL Server C#

    我有一个 270k 行的数据库 带有主键mid和一个名为value 我有一个包含中值和值的文本文件 现在我想更新表格 以便将每个值分配给正确的中间值 我当前的方法是从 C 读取文本文件 并为我读取的每一行更新表中的一行 必须有更快的方法来做
  • Visual Studio 中的测试单独成功,但一组失败

    当我在 Visual Studio 中单独运行测试时 它们都顺利通过 然而 当我同时运行所有这些时 有些通过 有些失败 我尝试在每个测试方法之间暂停 1 秒 但没有成功 有任何想法吗 在此先感谢您的帮助 你们可能有一些共享数据 检查正在使用
  • 将 log4net 与 Autofac 结合使用

    我正在尝试将 log4net 与 Autofac 一起使用 我粘贴了这段代码http autofac readthedocs org en latest examples log4net html http autofac readthed
  • (de)从 CSV 序列化为对象(或者最好是类型对象的列表)

    我是一名 C 程序员 试图学习 C 似乎有一些内置的对象序列化 但我在这里有点不知所措 我被要求将测试数据从 CSV 文件加载到对象集合中 CSV 比 xml 更受青睐 因为它更简单且更易于人类阅读 我们正在创建测试数据来运行单元测试 该集
  • C++ 密码屏蔽

    我正在编写一个代码来接收密码输入 下面是我的代码 程序运行良好 但问题是除了数字和字母字符之外的其他键也被读取 例如删除 插入等 我知道如何避免它吗 特q string pw char c while c 13 Loop until Ent
  • 为什么在setsid()之前fork()

    Why fork before setsid 守护进程 基本上 如果我想将一个进程与其控制终端分离并使其成为进程组领导者 我使用setsid 之前没有分叉就这样做是行不通的 Why 首先 setsid 将使您的进程成为进程组的领导者 但它也
  • 如何将 Roslyn 语义模型返回的类型符号名称与 Mono.Cecil 返回的类型符号名称相匹配?

    我有以下代码 var paramDeclType m semanticModel GetTypeInfo paramDecl Type Type Where paramDeclType ToString returns System Col

随机推荐

  • 机器人代码 + Python

    问题是 如何使用 Python 为 Robocode 制作机器人 似乎有两个选择 Robocode Jython 适用于 NET Iron Python 的 Robocode 第一个有一些信息 但看起来不是很可靠 而后者则没有 一步一步来
  • 构建多种架构的框架(arm64、armv7、armv7s)

    我正在尝试将针对arm64 armv7 和armv7s 的项目构建上传到TestFlight 它正在使用另一个项目的框架 但该框架似乎仅适用于arm64 而不适用于arm64 file was built for arm64 which i
  • Gulp TypeError:path.join 的参数必须是字符串

    我对 gulp ruby sass 有疑问 当我尝试运行时watch任务并更改一些 sass 文件时会出现错误 TypeError Arguments to path join must be strings 这是我的 gulpfile j
  • C++ 中简写“if”的 Python 等效项 [重复]

    这个问题在这里已经有答案了 有没有办法用Python编写这个C C 代码 a b true 123 456 a 123 if b else 456
  • 用于停止事件传播的 jQuery UI 滑块

    我在用jQuery用户界面 http en wikipedia org wiki JQuery UI Slider在可拖动的 div 中 单击滑块后 事件就会传播并且 div 开始拖动 有没有办法在鼠标按下事件时停止其传播 使用 retur
  • Bootstrap 3.0.0 破坏了日期选择器图标

    这里是Demo http plnkr co edit 6nUu7JkVYdTiWHbXhT3f p preview 如果您使用2 3 1 取消第12行的注释 您将看到按钮正确显示 有谁知道如何修复 3 0 0 rc1 版本 要修复 boot
  • 来自 png 图像的橱窗应用程序的表达式混合中的平滑动画

    我想在 Windows 应用商店应用程序的 Expression Blend 中制作动画 因为不支持 gif 图像 所以我有大约 30 张 png 图像 我在 Blend 中制作了一个故事板动画 方法是在时间轴中每 2 秒更改一次图像源 但
  • 为什么 C 数组尾部有多余的字节? [复制]

    这个问题在这里已经有答案了 我检查了 C 数组尾部可能有一些额外的字节 有我的代码 int a 5 int test 1 2 3 4 int b 5 test 1 11 test 4 11 cout lt lt b lt lt endl 1
  • 如何使用 IIS 7.5 压缩 ASP.NET MVC 的 Json 结果

    我很难让 IIS 7 正确压缩 ASP NET MVC 的 Json 结果 我在 IIS 中启用了静态和动态压缩 我可以使用 Fiddler 验证普通文本 html 和类似记录是否已压缩 查看请求 存在接受编码 gzip 标头 响应具有 m
  • Wordpress 登录页面重定向到普通用户的 Woocommerce 我的帐户页面

    我陷入了深深的麻烦之中 请帮我找到以下问题的解决方案 我有一个使用 Woocommerce 插件的 WordPress 网站 通常 Woocommerce 不允许普通用户 客户 用户 您可以从 Wordpress 管理区域 gt 添加新用户
  • 参数返回 SQL0418 的 iDB2 Select 命令

    我正在开发一个使用 IBM Data DB2 iSeries dll 连接到 DB2 iSeries 7 1 数据库的 NET 应用程序 我需要执行一个 SELECT 命令 该命令有 n 个参数 这些参数在查询中定义为 paramX 然后设
  • 将通配符传递给别名

    我使用修改列表命令作为别名 在 KSH 中 alias ltf ls lrt d 1 PWD 所以命令ltf显示类似这样的内容 rw r r 1 myuser mygroup 0 Apr 18 12 00 usr test txt rw r
  • 用C#编写0-1背包的模拟退火算法

    我正在学习模拟退火算法 并且有一些关于如何修改示例算法来解决 0 1 背包问题的问题 我在CP上发现了这段很棒的代码 http www codeproject com KB recipes simulatedAnnealingTSP asp
  • Google Cloud SQL 的典型 ormconfig.json 文件?

    我已经尝试了几个小时了 Google Cloud SQL 与 TypeORM 配合使用的 ormconfig json 文件应该是什么 我设法让它在本地使用数据库的IP 使用mysql工作台和Google云代理并将我的IP列入白名单 但我不
  • 如何等到跟踪脚本触发后再重定向用户?

    我运行一个典型的价格比较网站 用户浏览产品 然后单击链接转到商家的网站 在被重定向到商家的网站之前 用户会看到一个 我们正在重定向您 页面 此页面仅允许跟踪代码 Google Analytics Adwords Bing Ads 来跟踪事件
  • FirebaseRecyclerAdapter 具有用于删除项目的操作模式

    我正在尝试实现一个操作模式来删除 FirebaseRecyclerView 中的项目 奇怪的是 有时删除的项目并不是选定的项目 我认为错误出现在 ToogleSelection 方法或 RemoveItems 方法中 但我不知道它是什么 适
  • PyCharm 在同一文件夹中找不到导入

    我正在使用 PyCharm 并且从同一目录中的另一个 python 文件导入一些常量 导入在运行时起作用 但我在导入语句上以及每次使用文件中的常量时都会得到这个恼人的红色下划线 这是文件层次结构 请忽略文件夹上的红色下划线 它们与此无关 是
  • FileSystemWatcher 网络断开

    我有一个 FileSystemWatcher 监视网络共享上的文件 如果发生导致共享不可用的事件 可能是由于网络问题 FileSystemWatcher 将断开连接 显然我可以处理 错误 事件 也许可以做一些日志记录 并且很多文章建议在错误
  • Matplotlib,使用 imshow 刷新图像更快

    我正在开发一个项目 在该项目上 我必须在 GUI 窗口上绘制 320 250 像素的图像 如果可能的话 每秒绘制 60 次 所以我尝试这样做matplotlib 2 0 2 Python 3 6 and PyQt5 因为我开始了解这些工具并
  • 如果是嵌套元素,则后代 Until()

    在我之前的问题中 link https stackoverflow com questions 75957548 xdocument descendants cannot distinguish between parent child e