c# - linq to xml:读取xml配置文件

时间:2015-09-24 10:04:19

标签: c# xml linq linq-to-xml

我想在c#中加载一个config.xml文件,将其拆分为4个不同的列表。 XML文件如下所示:

    <?xml version="1.0" encoding="UTF-8"?>
    <Configs version="1.0" author="AP">
        <config ver="9.7">
            <start>00090[ABCDEF].*</start>
            <lines>544</lines>
            <configFile>cfg_9_7.xml</configFile>
        </config>
        <config ver="9.7_512">
            <start>00090[1-9].*</start>
            <lines>512</lines>
            <configFile>cfg_9_7_v2_512.xml</configFile>
        </config>
        <config ver="9.7">
            <start>00090[2-7].*</start>
            <lines>256</lines>
            <configFile>cfg_9_7_small.xml</configFile>
        </config>
    </Configs>

我需要拆分配置版本(config ver="..."),开头,行和configFile。所有列表都可以保存字符串,所以我只需要值和属性。我想用Linq转换为XML,因为我认为它比我的“Read all Lines and search for keywords”功能更快更小:

    var cfg = File.ReadAllLines(folder + keyCfg);
    List<String> config = new List<String>(cfg);
    Boolean keyFormatConfig = false;
    for (int i = 0; i < config.Count; i++)
    {
        String line = config[i];
        while (line.StartsWith(" ") || line.StartsWith("\t"))
        {
            line = line.Substring(1);
        }
        if (line.StartsWith("<config ver=\""))
        {
            keyFormatConfig = true;
        }
        if (line.StartsWith("</config>"))
        {
            keyFormatConfig = false;
        }
        if (keyFormatConfig)
        {
            if (line.StartsWith("<config ver=\""))
            {
                String[] name = line.Split('"');
                if (name.Length >= 2)
                {
                    Version.Add(name[1]);
                }
            }
            else if (line.StartsWith("<start>"))
            {
                line = line.Substring(7);
                if (line.Contains("<"))
                {
                    String[] value = line.Split('<');
                    Start.Add(value[0]);
                }
            }
            else if (line.StartsWith("<lines>"))
            {
                line = line.Substring(7);
                if (line.Contains("<"))
                {
                    String[] value = line.Split('<');
                    Lines.Add(value[0]);
                }
            }
            else if (line.StartsWith("<configFile>"))
            {
                line = line.Substring(12);
                if (line.Contains("<"))
                {
                    String[] value = line.Split('<');
                    ConfigFile.Add(value[0]);
                }
            }
        }
    }

抱歉我的英语不好:)

2 个答案:

答案 0 :(得分:4)

一个简单的LINQ:

void Main()
{
    var configFile = @"c:\temp\so\config.xml";
    var xdoc = XDocument.Load(configFile);
    var configs = xdoc.Element("Configs").Elements("config");
    foreach (var c in configs)
    {
        Console.WriteLine (c.Attribute("ver").Value);
        Console.WriteLine ("Start {0}",c.Element("start").Value);
    }
}

运行它会产生

9.7
Start 00090[ABCDEF].*
9.7_512
Start 00090[1-9].*
9.7
Start 00090[2-7].*

答案 1 :(得分:2)

您可以像这样使用XDocument

XDocument doc = XDocument.Load(yourFile);

var configs = doc.Descendants("config").Select(i => new Config()
{
    Version = i.Attribute("ver").Value,
    Start = i.Element("start").Value,
    Lines = i.Element("lines").Value,
    ConfigFile = i.Element("configFile").Value,
}).ToList();

这是Config

public class Config
{
    public string Start { get; set; }

    public string Lines { get; set; }   //Also you may want to use int

    public string ConfigFile { get; set; }

    public string Version { get; set; }
}