通过从 xml-File 中选择一个块来获取数组
Get an Array by selection of a block from xml-File
我必须使用 Linq 编写程序。我只是一个学生,我还没有学过,所以我有两个问题:
- 什么是好的book/ebook...自学我的下一个问题是什么?
我有一个 XML-文件,看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<Projects>
<Project>
<Information>
<Name>Project1</Name>
<Date>26.01.2015</Date>
</Information>
<Files ID = "S" path = "C:\Users\marcel\Documents">
<file>Test1.txt</file>
<file>Test2.txt</file>
<file>Test3.txt</file>
<file>Test4.txt</file>
<file>Test5.txt</file>
</Files>
<Files ID = "C" path = "C:\Users\marcel\Documents">
<file>Test1(1).txt</file>
<file>Test1(2).txt</file>
<file>Test1(3).txt</file>
<file>Test1(4).txt</file>
<file>Test1(5).txt</file>
</Files>
</Project>
我想要一个字符串数组,其中包含 "file" 元素的值,具体取决于 ID=S 或 C。
我在那里有超过 1 个项目,所以首先必须按名称搜索它,现在正在工作:
var entries = from items in xelement.Elements("Project")
where (string)items.Element("Name").Value == projectName
select items;
这让我得到了所需项目的整个模块。
我可以使用第一个命令的结果来获取文件名吗?
或者我可以只扩展第一部分的代码吗?
要获取具有指定名称的特定 Project
元素,您可以使用 First
:
var projectElement = xElement
.Elements("Project")
.First(x => (String) x.Element("Information").Element("Name").Value == projectName);
以类似的方式,您可以通过指定 ID 属性的值来找到所需的 Files
元素:
var filesElement = projectElement
.Elements("Files")
.First(x => x.Attribute("ID").Value == id);
然后您可以使用 Select
将 File
元素投影为其值并将其转换为数组:
var files = filesElement
.Elements("file")
.Select(x => (String) x.Value)
.ToArray();
请注意,如果 XML 具有意外格式,此代码将抛出异常。例如,如果 First
没有找到匹配的元素,则会抛出异常。此外,如果未找到指定的元素,Element
方法将 return null
,因此如果没有 Information
元素,x.Element("Information").Element("Name")
之类的代码将抛出异常因为对 Element
的下一次调用是在 null
引用上执行的。
谢谢 Martin,成功了 :)
我刚刚想出了一个自己的解决方案,如下所示:
var files = from file in entries.Elements("Files").Elements("file")
where (string)file.Parent.Attribute("ID").Value == cOrS
select file.Value;
我必须使用 Linq 编写程序。我只是一个学生,我还没有学过,所以我有两个问题:
- 什么是好的book/ebook...自学我的下一个问题是什么?
我有一个 XML-文件,看起来像这样:
<?xml version="1.0" encoding="utf-8"?> <Projects> <Project> <Information> <Name>Project1</Name> <Date>26.01.2015</Date> </Information> <Files ID = "S" path = "C:\Users\marcel\Documents"> <file>Test1.txt</file> <file>Test2.txt</file> <file>Test3.txt</file> <file>Test4.txt</file> <file>Test5.txt</file> </Files> <Files ID = "C" path = "C:\Users\marcel\Documents"> <file>Test1(1).txt</file> <file>Test1(2).txt</file> <file>Test1(3).txt</file> <file>Test1(4).txt</file> <file>Test1(5).txt</file> </Files> </Project>
我想要一个字符串数组,其中包含 "file" 元素的值,具体取决于 ID=S 或 C。 我在那里有超过 1 个项目,所以首先必须按名称搜索它,现在正在工作:
var entries = from items in xelement.Elements("Project")
where (string)items.Element("Name").Value == projectName
select items;
这让我得到了所需项目的整个模块。 我可以使用第一个命令的结果来获取文件名吗? 或者我可以只扩展第一部分的代码吗?
要获取具有指定名称的特定 Project
元素,您可以使用 First
:
var projectElement = xElement
.Elements("Project")
.First(x => (String) x.Element("Information").Element("Name").Value == projectName);
以类似的方式,您可以通过指定 ID 属性的值来找到所需的 Files
元素:
var filesElement = projectElement
.Elements("Files")
.First(x => x.Attribute("ID").Value == id);
然后您可以使用 Select
将 File
元素投影为其值并将其转换为数组:
var files = filesElement
.Elements("file")
.Select(x => (String) x.Value)
.ToArray();
请注意,如果 XML 具有意外格式,此代码将抛出异常。例如,如果 First
没有找到匹配的元素,则会抛出异常。此外,如果未找到指定的元素,Element
方法将 return null
,因此如果没有 Information
元素,x.Element("Information").Element("Name")
之类的代码将抛出异常因为对 Element
的下一次调用是在 null
引用上执行的。
谢谢 Martin,成功了 :) 我刚刚想出了一个自己的解决方案,如下所示:
var files = from file in entries.Elements("Files").Elements("file")
where (string)file.Parent.Attribute("ID").Value == cOrS
select file.Value;