简体   繁体   English

C#使用xdocument将多个元素添加到列表

[英]C# Adding multiple elements to a list using xdocument

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <fileUploadSpecification>
    <DirectoryPath>C:\watchFolder</DirectoryPath>
    <Region>us-west-2</Region>
    <UploadBucket>configurationtestbucket</UploadBucket>
    <FileType>
      <type>*.txt</type>
      <type>*.OpticomCfg</type>
    </FileType>
  </fileUploadSpecification>
  <fileUploadSpecification>
    <DirectoryPath>C:\watchFolder</DirectoryPath>
    <Region>us-west-2</Region>
    <UploadBucket>loguploadbucket</UploadBucket>
    <FileType>
      <type>*.Xml</type>
      <type>*.Json</type>
    </FileType>
  </fileUploadSpecification>
</root>

This is the XML file I need to parse, I want to get each instance of fileUploadSpecification so that I can put each set of details into a list, I think some type of for loop would be appropriate, where I loop through and add the first set of upload details and then loop through and add the second. 这是我需要解析的XML文件,我想获取fileUploadSpecification的每个实例,以便可以将每组详细信息放到一个列表中,我认为某种类型的for循环将是适当的,在此循环并添加第一个一组上传详细信息,然后循环浏览并添加第二个。 This is what I currently have, but it never gets to the second fileUploadSpecification element, it just returns the same one again. 这是我目前拥有的,但是它永远不会到达第二个fileUploadSpecification元素,它只会再次返回相同的内容。 The idea would be to create a new SettingsData for every set of fileUploadSpecification elements, whether it be two like shown above, or 10. 这个想法是为每个fileUploadSpecification元素集创建一个新的SettingsData,无论它是上面显示的两个还是10。

public interface ISettingsEngine
{

    IEnumerable<SettingsData> GetSettings();
}

public class SettingsEngine : ISettingsEngine
{

    public IEnumerable<SettingsData> GetSettings()
    {
        List<SettingsData> dataList = new List<SettingsData>();
        try
        {

            var xDoc = XDocument.Load("File1.xml");

            var instancesToParse = xDoc.Root.Elements().Count();

            var fileCount = xDoc.Root.Elements("FileType").Count();


            for (int x = 0; x < instancesToParse; x++)
            {
                var newSettingsData = new SettingsData();

                newSettingsData.UploadBucket = xDoc.Root.Element("fileUploadSpecification").Element("UploadBucket").Value;
                newSettingsData.Region = xDoc.Root.Element("fileUploadSpecification").Element("Region").Value;
                newSettingsData.DirectoryPath = xDoc.Root.Element("fileUploadSpecification").Element("DirectoryPath").Value;
                var query = xDoc.Root.Descendants("FileType").Elements("type");

                foreach (XElement e in query)
                {
                    newSettingsData.FileType.Add(e.Value);
                }
dataList.Add(newSettingsData);

            }

            return dataList;
        }
        catch(Exception)
        {
            return dataList;
        }
    }
}

public class SettingsData
{

    public List<string> FileType { get; set; }
    public string DirectoryPath { get; set; }
    public string Region { get; set; }
    public string UploadBucket { get; set; }

    public SettingsData()
    {
        FileType = new List<string>();
    }
}
var dataList = (from fus in xDoc.Root.Elements("fileUploadSpecification")
             select new SettingsData
             {
                 UploadBucket = fus.Element("UploadBucket").Value,
                 Region = fus.Element("Region").Value,
                 DirectoryPath = fus.Element("DirectoryPath").Value,
                 FileType = fus.Element("FileType")
                               .Elements("type").Select(f =>f.Value).ToList()
             }).ToList();

Each time through the loop, you're looking up the first fileUploadSpecification element all over again. 每次循环时,您都会重新查找第一个fileUploadSpecification元素。 You used the Elements() method already, in a few places. 您已经在几个地方使用了Elements()方法。 That's the one you want. 那就是你想要的那个。 Always favor foreach over for in C#, when you're looping over a collection. 总是青睐foreach超过for在C#中,当你遍历集合。 It's quicker (to code, not at runtime) and less error prone. 它更快(编写代码,而不是在运行时)并且出错的可能性更少。

foreach (var uploadSpec in xDoc.Root.Elements("fileUploadSpecification"))
{
    var newSettingsData = new SettingsData();

    newSettingsData.UploadBucket = uploadSpec.Element("UploadBucket").Value;
    newSettingsData.Region = uploadSpec.Element("Region").Value;
    newSettingsData.DirectoryPath = uploadSpec.Element("DirectoryPath").Value;

    var types = uploadSpec.Descendants("FileType").Elements("type").Select(e => e.Value);

    foreach (var type in types)
    {
        newSettingsData.FileType.Add(type);
    }

    //  Or if newSettingsData.FileType is List<String>...
    //newSettingsData.FileType.AddRange(types);

    dataList.Add(newSettingsData);
}

James Curran's answer is functionally the same, but it's better form. 詹姆斯·柯兰(James Curran)的回答在功能上是相同的,但它是更好的形式。

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

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