【问题标题】:XML Attributes of C# Class - attribute and valueC# 类的 XML 属性 - 属性和值
【发布时间】:2018-11-05 21:58:37
【问题描述】:

我正在尝试将 C# 类序列化为以下内容:

<ns3:Test ab="11111">Test 123</ns3:Test>

我的班级是这样的:

[XmlType(Namespace = "http://foo")]
public class Test
{
    [XmlAttribute(AttributeName = "ab")]
    public string Ab { get; set; } = "11111";

    [XmlElement("Test")]
    public string Test1 { get; set; } = "Test 123";
}

我的输出如下所示:

  <ns3:Test tc="11111">
    <ns3:Test>Test 123</ns3:Test>
  </ns3:Test>

我的类定义显然是错误的。有没有办法使用注释来实现我正在寻找的东西?

【问题讨论】:

  • 您发布的内容是不可能的。重新编译您的代码,然后重试。在项目 bin 文件夹中有一个调试和发布文件夹。通过重新编译并检查 exe 文件的时间戳,确保您运行的是正确的版本。

标签: c# xml serialization


【解决方案1】:

只需将Test1的属性替换为:

[XmlText]
public string Test1 { get; set; } = "Test 123";

【讨论】:

  • 谢谢!正是我所缺少的!
猜你喜欢
  • 1970-01-01
  • 2020-05-20
  • 2012-02-18
  • 2011-03-18
  • 2021-11-24
  • 1970-01-01
  • 2021-11-17
  • 2011-11-06
相关资源
最近更新 更多