[英]Using xml for a configuration file
我試圖弄清楚如何將XmlTextReader
和XmlTextWriter
用於程序的配置文件。
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>
這是我到目前為止的課程:
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/zh-cn/library/system.xml.serialization.xmlserializer.aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.