简体   繁体   English

通过从xml文件中选择一个块来获取数组

[英]Get an Array by selection of a block from xml-File

I have to write a program using Linq. 我必须使用Linq编写程序。 I'm just a student and I didn't learned that yet, so I have two questions: 我只是一个学生,还没有学到,所以我有两个问题:

  1. What would be a good book/ebook... to teach myself what my next question will be about? 一本好书/电子书...可以教自己下一个问题是什么?
  2. I have an XML-File, that looks like this: 我有一个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> 

I want to get a string-array which is containing the values of the "file" elements, depenging on ID=S or C. I have more than 1 project in there, so it first has to be searched by name, that's working right now: 我想得到一个包含“文件”元素值的字符串数组,依赖于ID = S或C。我在那里有多个项目,因此首先必须按名称搜索,这是正确的现在:

var entries = from items in xelement.Elements("Project")
               where (string)items.Element("Name").Value == projectName
               select items;

that gets me the whole block of the needed project. 这使我获得了所需项目的整个模块。 Can I use the result of the first command for getting the filenames? 我可以使用第一个命令的结果来获取文件名吗? Or can I just extend the code of the first part? 还是可以仅扩展第一部分的代码?

To get a specific Project element having the specified name you can use First : 要获得具有指定名称的特定Project元素,可以使用First

var projectElement = xElement
  .Elements("Project")
  .First(x => (String) x.Element("Information").Element("Name").Value == projectName);

In a similar way you can find the desired Files element by specifying a value for the ID attribute: 通过类似的方式,您可以通过指定ID属性的值来找到所需的Files元素:

var filesElement = projectElement
  .Elements("Files")
  .First(x => x.Attribute("ID").Value == id);

You can then use Select to project the File elements to their values and convert that to an array: 然后,您可以使用SelectFile元素投影到其值并将其转换为数组:

var files = filesElement
  .Elements("file")
  .Select(x => (String) x.Value)
  .ToArray();

Note that this code will throw exceptions if the XML has an unexpected format. 请注意,如果XML具有意外格式,则此代码将引发异常。 Eg, if First does not find a matching element an exception is thrown. 例如,如果First找不到匹配的元素,则会引发异常。 Also, the Element method will return null if the specified element is not found and thus code like x.Element("Information").Element("Name") will throw an exception if there is no Information element because the next call to Element is performed on the null reference. 另外,如果未找到指定的元素,则Element方法将返回null ,因此,如果没有Information元素,则x.Element("Information").Element("Name")将引发异常,因为下一次对Element调用对null引用执行。

Thank you Martin, that worked :) I just came up with an own solution looking like this: 谢谢Martin,这个方法很有效:)我只是想出了一个自己的解决方案,如下所示:

var files = from file in entries.Elements("Files").Elements("file")
                        where (string)file.Parent.Attribute("ID").Value == cOrS
                        select file.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM