c# - 使用 xml 作为配置文件

标签 c# xml

我正在尝试弄清楚如何使用 XmlTextReaderXmlTextWriter用于我的程序的配置文件。

xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Base>
    <Global>
        <some_config_value>86400</some_config_value>
        <test1>t_1</test1>
        <test2>t_2</test2>
        <test3>t_3</test3>
        <test4>t_4</test4>
    </Global>
    <test_head>
        <test5>t_5</test5>
        <test6>t_6</test6>
    </test_head>
</Base>

这是我目前的类(class):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace my_program.Global
{
    class CXMLConfig
    {
        private string path;

        public CXMLConfig(string filepath)
        {
            path = filepath;
        }

        public string XmlReadValue(string Section, string Key)
        {
            XmlTextReader textReader = new XmlTextReader(path);
            ReadElements readEl = new ReadElements(textReader, Section, Key);
            textReader.Close();
            return readEl.Value;
        }

        private class ReadElements
        {
            XmlTextReader textReader;
            string Section;
            string Key;

            private bool inBase = false;
            private bool inSection = false;
            private bool inKey = false;

            public string Value { get; private set; }

            public ReadElements(XmlTextReader textReader_set, string Section_set, string Key_set)
            {
                Value = "";

                this.textReader = textReader_set;
                this.Section = Section_set;
                this.Key = Key_set;

                textReader.Read();
                while (textReader.Read())
                {
                    // Move to fist element
                    textReader.MoveToElement();

                    string nodetype = textReader.NodeType.ToString();

                    if (textReader.LocalName == "Base")
                    {
                        if (nodetype == "Element")
                        {
                            if (!inBase)
                                inBase = true;
                        }
                        else if (nodetype == "EndElement")
                        {
                            if (inBase)
                                inBase = false;
                        }
                    }
                    else if (inBase && textReader.LocalName == Section)
                    {
                        if (nodetype == "Element")
                        {
                            if (!inSection)
                                inSection = true;
                        }
                        else if (nodetype == "EndElement")
                        {
                            if (inSection)
                                inSection = false;
                        }
                    }
                    else if (inBase && inSection && textReader.LocalName == Key)
                    {
                        if (inSection)
                        {
                            if (nodetype == "Element")
                            { 
                                if (!inKey)
                                    inKey = true;
                            }
                            else if (nodetype == "EndElement")
                            {
                                if (inKey)
                                    inKey = false;
                            }
                        }
                    }
                    else if (inBase && inSection && inKey)
                    {
                        if (nodetype == "Text")
                        {
                            Value = textReader.Value.ToString();
                            //Console.WriteLine(Value);
                        }
                    }
                }
            }

        }
    }
}

所以首先,这可能是糟糕的 XML。我以前从未使用过它,它看起来确实有点……奇怪。然后就是我写了整个 ReadElements类从配置文件中读取一个值,但我想有一种更简单的方法可以用 XmlTextReader 来做到这一点(但我找不到它)。最后,我还没有弄清楚如何使用 XmlTextWriter 更新 xml 文件中的值。无需从上到下重写整个 xml 文件。

最佳答案

您可以使用 XmlSerializer 序列化和反序列化任意配置类。这是一个示例实现:

public enum MyEnum
{
    ValueA,
    ValueB
}

[Serializable]
public class Configuration : PersistableObject
{
    public double A { get; set; }
    public string B { get; set; }
    public MyEnum C { get; set; }
}

public class PersistableObject
{
    public static T Load<T>(string fileName) where T : PersistableObject, new()
    {
        T result = default(T);

        using (FileStream stream = File.OpenRead(fileName))
        {
            result = new XmlSerializer(typeof(T)).Deserialize(stream) as T;
        }

        return result;
    }

    public void Save<T>(string fileName) where T : PersistableObject
    {
        using (FileStream stream = new FileStream(fileName, FileMode.CreateNew))
        {
            new XmlSerializer(typeof(T)).Serialize(stream, this);
        }
    }
}

有关如何配置 XmlSerializer 的更多信息,请查看 MSDN 文章:http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

关于c# - 使用 xml 作为配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11627672/

相关文章:

xml - 如何使 Xml 模式中的元素成为可选元素

java - Jackson XML + 不要将值包装在标签中

java - Android Java实现1个页面2个按钮

php - 如何使用 Codeigniter 在浏览器中查看 XML 文件

c# - 使用asp的excel行数

c# - 如何在C#中实现MATLAB 'tansig'双曲正切sigmoid传递函数

c# - 在发布期间包含文件

c# - 带参数的DataReader;

c# - 在 Unity 中使用资源文件夹

c# - XslCompiledTransform 使用 UTF-16 编码