使用 IXmlSerialized 类表示 XML 配置文件

发布于 2024-09-06 02:09:31 字数 2841 浏览 5 评论 0原文

我正在用 C# 编写并尝试通过 IXmlSerialized 类表示 XML 配置文件。不过,我不确定如何表示配置文件中的嵌套元素,例如 logLevel

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <logging>
    <logLevel>Error</logLevel>
  </logging>
  <credentials>
    <user>user123</user>
    <host>localhost</host>
    <password>pass123</password>
  </credentials>
  <credentials>
    <user>user456</user>
    <host>my.other.domain.com</host>
    <password>pass456</password>
  </credentials>
</configuration>

有一个名为 LogLevel 的枚举,它表示 logLevel 的所有可能值代码>logLevel标签。 credentials 中的标签应全部以字符串形式出现。在我的名为 DLLConfigFile 的类中,我有以下内容:

[XmlElement(ElementName="logLevel", DataType="LogLevel")]
public LogLevel LogLevel;

但是,这不会起作用,因为 不在 XML 的根节点内文件中,它是 更深的一个节点。我该怎么做呢?

至于 节点,我的猜测是我需要第二个类,例如 CredentialsSection,并具有如下属性:

[XmlElement(ElementName="credentials", DataType="CredentialsSection")]
public CredentialsSection[] AllCredentials;

编辑: 好吧,我尝试了 Robert Love 的建议并创建了一个 LoggingSection 类。但是,我的测试失败了:

var xs = new XmlSerializer(typeof(DLLConfigFile));

using (var stream = new FileStream(_configPath, FileMode.Open,
    FileAccess.Read, FileShare.Read))
{
    using (var streamReader = new StreamReader(stream))
    {
        XmlReader reader = new XmlTextReader(streamReader);
        var file = (DLLConfigFile)xs.Deserialize(reader);
        Assert.IsNotNull(file);
        LoggingSection logging = file.Logging;
        Assert.IsNotNull(logging); // fails here
        LogLevel logLevel = logging.LogLevel;
        Assert.IsNotNull(logLevel);
        Assert.AreEqual(EXPECTED_LOG_LEVEL, logLevel);
    }
}

我正在测试的配置文件肯定有 。这些类如下所示:

[Serializable]
[XmlRoot("logging")]
public class LoggingSection : IXmlSerializable
{
    public XmlSchema GetSchema()
    {
        return null;
    }

    [XmlElement(ElementName="logLevel", DataType="LogLevel")]
    public LogLevel LogLevel;

    public void ReadXml(XmlReader reader)
    {
        LogLevel = (LogLevel)Enum.Parse(typeof(LogLevel),
            reader.ReadString());
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteString(Enum.GetName(typeof(LogLevel), LogLevel));
    }
}

[Serializable]
[XmlRoot("configuration")]
public class DLLConfigFile : IXmlSerializable
{
    [XmlElement(ElementName="logging", DataType="LoggingSection")]
    public LoggingSection Logging;
}

I'm writing in C# and trying to represent an XML config file through an IXmlSerializable class. I'm unsure how to represent the nested elements in my config file, though, such as logLevel:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <logging>
    <logLevel>Error</logLevel>
  </logging>
  <credentials>
    <user>user123</user>
    <host>localhost</host>
    <password>pass123</password>
  </credentials>
  <credentials>
    <user>user456</user>
    <host>my.other.domain.com</host>
    <password>pass456</password>
  </credentials>
</configuration>

There is an enum called LogLevel that represents all the possible values for the logLevel tag. The tags within credentials should all come out as strings. In my class, called DLLConfigFile, I had the following:

[XmlElement(ElementName="logLevel", DataType="LogLevel")]
public LogLevel LogLevel;

However, this isn't going to work because <logLevel> isn't within the root node of the XML file, it's one node deeper in <logging>. How do I go about doing this?

As for the <credentials> nodes, my guess is I will need a second class, say CredentialsSection, and have a property such as the following:

[XmlElement(ElementName="credentials", DataType="CredentialsSection")]
public CredentialsSection[] AllCredentials;

Edit: okay, I tried Robert Love's suggestion and created a LoggingSection class. However, my test fails:

var xs = new XmlSerializer(typeof(DLLConfigFile));

using (var stream = new FileStream(_configPath, FileMode.Open,
    FileAccess.Read, FileShare.Read))
{
    using (var streamReader = new StreamReader(stream))
    {
        XmlReader reader = new XmlTextReader(streamReader);
        var file = (DLLConfigFile)xs.Deserialize(reader);
        Assert.IsNotNull(file);
        LoggingSection logging = file.Logging;
        Assert.IsNotNull(logging); // fails here
        LogLevel logLevel = logging.LogLevel;
        Assert.IsNotNull(logLevel);
        Assert.AreEqual(EXPECTED_LOG_LEVEL, logLevel);
    }
}

The config file I'm testing with definitely has <logging>. Here's what the classes look like:

[Serializable]
[XmlRoot("logging")]
public class LoggingSection : IXmlSerializable
{
    public XmlSchema GetSchema()
    {
        return null;
    }

    [XmlElement(ElementName="logLevel", DataType="LogLevel")]
    public LogLevel LogLevel;

    public void ReadXml(XmlReader reader)
    {
        LogLevel = (LogLevel)Enum.Parse(typeof(LogLevel),
            reader.ReadString());
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteString(Enum.GetName(typeof(LogLevel), LogLevel));
    }
}

[Serializable]
[XmlRoot("configuration")]
public class DLLConfigFile : IXmlSerializable
{
    [XmlElement(ElementName="logging", DataType="LoggingSection")]
    public LoggingSection Logging;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怂人 2024-09-13 02:09:31

创建一个具有单个属性 LogLevel 的子类,然后在新类型的基类上创建一个日志记录属性。

Create a subclass that has a single property LogLevel and then create a logging property on your base class that is of the the new type.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文