检索数据从XML文件 [英] Retrieving Data From XML File

查看:134
本文介绍了检索数据从XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有与检索用C#,我知道这是因为我的非常有限的C#和.XML知识XML值的问题。

I seem to be having a problem with retrieving XML values with C#, which I know it is due to my very limited knowledge of C# and .XML.

我得到了下面的XML文件

I was given the following XML file

<PowerBuilderRunTimes>
    <PowerBuilderRunTime>
        <Version>12</Version>
        <Files>
            <File>EasySoap110.dll</File>
            <File>exPat110.dll</File>
            <File>pbacc110.dll</File>
        </File>
     </PowerBuilderRunTime>
</PowerBuilderRunTimes>

我来处理XML文件,并确保每个文件夹中存在的文件(这是比较容易的部分)。这是我已经有一个很难的XML文件的处理。以下是我迄今所做:

I am to process the XML file and make sure that each of the files in the exist in the folder (that's the easy part). It's the processing of the XML file that I have having a hard time with. Here is what I have done thus far:

var runtimeXml = File.ReadAllText(string.Format("{0}\\{1}", configPath, Resource.PBRuntimes));

var doc = XDocument.Parse(runtimeXml);
var topElement = doc.Element("PowerBuilderRunTimes");
var elements = topElement.Elements("PowerBuilderRunTime");

foreach (XElement section in elements)
{
    //pbVersion is grabbed earlier. It is the version of PowerBuilder
    if( section.Element("Version").Value.Equals(string.Format("{0}", pbVersion ) ) )
    {
        var files = section.Elements("Files");

        var fileList = new List<string>();

        foreach (XElement area in files)
        {
            fileList.Add(area.Element("File").Value);
        }
    }
}

我的问题是一个只能填充一个值的字符串列表,EasySoap110.dll,以及其他一切被忽略。是否有人可以帮助我,我很茫然。

My issue is that the String List is only ever populated with one value, "EasySoap110.dll", and everything else is ignored. Can someone please help me, as I am at a loss.

推荐答案

看这一点:

var files = section.Elements("Files");

var fileList = new List<string>();

foreach (XElement area in files)
{
    fileList.Add(area.Element("File").Value);
}

您正在遍历每个文件元素,然后找出其中的第一个文件元素。只有一个文件元素 - 你必须遍历内的文件元素

You're iterating over each Files element, and then finding the first File element within it. There's only one Files element - you need to be iterating over the File elements within that.

但是,也有绝对这样做的更好的方法。例如:

However, there are definitely better ways of doing this. For example:

var doc = XDocument.Load(Path.Combine(configPath, Resource.PBRuntimes));
var fileList = (from runtime in doc.Root.Elements("PowerBuilderRunTime")
                where (int) runtime.Element("Version") == pbVersion
                from file in runtime.Element("Files").Elements("File")
                select file.Value)
               .ToList();

请注意,如果有的匹配 PowerBuilderRunTime 元素,将创建的所有的中的文件列表所有的那些元素。这可能不是你想要的。例如,您可能希望:

Note that if there are multiple matching PowerBuilderRunTime elements, that will create a list with all the files of all those elements. That may not be what you want. For example, you might want:

var doc = XDocument.Load(Path.Combine(configPath, Resource.PBRuntimes));
var runtime = doc.Root
                 .Elements("PowerBuilderRunTime")
                 .Where(r => (int) r.Element("Version") == pbVersion)
                 .Single();

var fileList = runtime.Element("Files")
                      .Elements("File")
                      .Select(x => x.Value)
                      .ToList();

这将验证有完全的一个的匹配运行。

That will validate that there's exactly one matching runtime.

这篇关于检索数据从XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆