LINQ to XML 查询同级元素

发布于 2024-11-26 09:56:53 字数 1374 浏览 1 评论 0原文

很抱歉,如果这个问题非常基本,但我没有太多使用 XML,这是我第一次使用 LINQ to XML...

我有一个 XML 站点地图,其结构类似于目录树:

<Site>
    <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
    <Folder name="FolderName">
        <Security>
            <Role>Admin</role>
        </Security>
        <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
        <Folder name="subFoler">
            <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
            <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
            <Folder>
                <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
            </Folder>
        </Folder>
    </Folder>
</Site>

这样,每个文件都会从其父文件夹(或祖父文件夹等)继承安全性,具体取决于 在树上的位置。

我希望能够,给定文件的 GUID,选择该文件使用 LINQ 编写文件,并收集与该文件继承的安全性相关的所有角色,

这是我迄今为止所做的,但这是一个非常粗略的尝试,并且并不完整:

XDocument sitemap = XDocument.Load(@"\sitemap.xml");
XElement currentFile = new XElement("File", 
        from file in sitemap.Elements("File")
        where file.Element("GUID").Equals(guid)
        select file.Parent.Element("security").Elements("role"));

ps.sitemap 文件位于同一个文件中。目录作为编写此代码的类

Sorry if this question is very basic, but I haven't worked with XML very much, and this is my first time working with LINQ to XML...

I have an XML sitemap that is structured like a directory tree:

<Site>
    <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
    <Folder name="FolderName">
        <Security>
            <Role>Admin</role>
        </Security>
        <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
        <Folder name="subFoler">
            <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
            <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
            <Folder>
                <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
            </Folder>
        </Folder>
    </Folder>
</Site>

In this way, every file will inherit security from its parent folder (or grandparent folder, etc, depending upon how far up the tree the <security></security> is located.

I want to be able to, given a file's GUID, select that file with LINQ, and gather all of the roles associated with the security that file has inherited.

This is what I have so far, but it is a very rough attempt, and is not complete:

XDocument sitemap = XDocument.Load(@"\sitemap.xml");
XElement currentFile = new XElement("File", 
        from file in sitemap.Elements("File")
        where file.Element("GUID").Equals(guid)
        select file.Parent.Element("security").Elements("role"));

ps. The sitemap file is located within the same directory as the class in which this code is written

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑咖 2024-12-03 09:56:53

看起来您想要沿着文件夹祖先链向上走并积累沿途找到的角色。您可以使用适当命名的 Ancestors() 和 < a href="http://msdn.microsoft.com/en-us/library/bb534336.aspx" rel="nofollow noreferrer">SelectMany() 方法:

XElement fileFromMap = sitemap.Descendants("File").Where(
    file => file.Attribute("GUID").Value == guid.ToString("D")).Single();

XElement currentFile = new XElement("File",
    fileFromMap.Value,
    fileFromMap.Ancestors("Folder").SelectMany(
        folder => {
            XElement security = folder.Element("Security");
            return (security != null
                ? security.Elements("Role") : new XElement[0]);
        }));

编辑:更改元素大写以匹配更新的问题。

编辑2:上面的代码错误地将null项提供给SelectMany()(这远没有XElement.Add那么放纵) () 在这方面),并且此 导致了 NullPointerException。它已更新为返回空的 XElement 数组。

It looks like you want to walk up the folder ancestor chain and accumulate the roles you find along the way. You can do that with the aptly-named Ancestors() and SelectMany() methods:

XElement fileFromMap = sitemap.Descendants("File").Where(
    file => file.Attribute("GUID").Value == guid.ToString("D")).Single();

XElement currentFile = new XElement("File",
    fileFromMap.Value,
    fileFromMap.Ancestors("Folder").SelectMany(
        folder => {
            XElement security = folder.Element("Security");
            return (security != null
                ? security.Elements("Role") : new XElement[0]);
        }));

EDIT: Changed element capitalization to match the updated question.

EDIT 2: The code above was mistakenly feeding a null item to SelectMany() (which is far less indulgent than XElement.Add() in this regard), and this resulted in a NullPointerException. It has been updated to return an empty XElement array instead.

来世叙缘 2024-12-03 09:56:53

试试这个(我通常使用方法语法,但你应该能够转换为查询语法)

        XElement currentFile = new XElement("File",
            sitemap.Descendants("file").Where(
               file => file.Attribute("GUID").Value.Equals(guid)).Select(
                   file => file.Parent.Element("security").Elements("role")));

这里 currentFile 应该有

<File>
  <role>Admin</role>
</File>

Try this (I usually do Method syntax but you should be able to convert to query syntax)

        XElement currentFile = new XElement("File",
            sitemap.Descendants("file").Where(
               file => file.Attribute("GUID").Value.Equals(guid)).Select(
                   file => file.Parent.Element("security").Elements("role")));

Here currentFile should have

<File>
  <role>Admin</role>
</File>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文