c# - 反序列化具有字符串属性的 bool 元素

标签 c# .net xml serialization xml-serialization

我正在尝试构建一个可以映射到此 XML 结构的类,但不知道如何构建。我在这里看到了一些例子,如果元素值是一个字符串,我可以使用 [XmlText]。在我的例子中,元素值是 bool 值。我应该如何构建我的“服务”类?

(我认为)我知道如何处理“服务”元素 :-)。它只是一组“服务”对象。我只是不确定如何构建“服务”类。

<?xml version="1.0" encoding="utf-8"?>
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MyConfigs>
    <Services>
        <Service Name="ServiceName">true</Service>
    </Services>
</MyConfigs>

我有这个:

[XmlArray("Services")]
[XmlArrayItem("Service")]
public Service[] Services { get; set; }

还有这个:

public class Service
{
    [XmlAttribute]
    public string Name { get; set; }
    // How do I get the boolean value here?????
}

最佳答案

尝试

[XmlText]
public bool ServiceValue {get;set;}

关于c# - 反序列化具有字符串属性的 bool 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11798281/

相关文章:

c# - 对 Windows Azure VPS 上的高 I/O 应用程序进行负载平衡

c# - 将私钥添加到 X509Certificate

c# - 将 XML 反序列化为不同元素上的相同类型的子元素

c# - 康威的生命游戏 - 初始化

c# - xml 根目录中的多个命名空间

c# - XML 格式错误

c# - Visual C# 窗体右键单击按钮

c# - 如何在 DataReader 上实现 Peek() 函数?

c# - 如何使用 C# 从 ASP.NET 检索 MAC id

java - 无法使用 jackson 将 xml 绑定(bind)到 pojo,com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException : Unrecognized field