LINQ to XML 查询同级元素
很抱歉,如果这个问题非常基本,但我没有太多使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想要沿着文件夹祖先链向上走并积累沿途找到的角色。您可以使用适当命名的 Ancestors() 和 < a href="http://msdn.microsoft.com/en-us/library/bb534336.aspx" rel="nofollow noreferrer">SelectMany() 方法:
编辑:更改元素大写以匹配更新的问题。
编辑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:
EDIT: Changed element capitalization to match the updated question.
EDIT 2: The code above was mistakenly feeding a
null
item toSelectMany()
(which is far less indulgent thanXElement.Add()
in this regard), and this resulted in aNullPointerException
. It has been updated to return an emptyXElement
array instead.试试这个(我通常使用方法语法,但你应该能够转换为查询语法)
这里 currentFile 应该有
Try this (I usually do Method syntax but you should be able to convert to query syntax)
Here currentFile should have