简体   繁体   English

C#类的XML属性-属性和值

[英]XML Attributes of C# Class - attribute and value

I am trying to serialize a C# class into the following: 我正在尝试将C#类序列化为以下内容:

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

My class looks like this: 我的课看起来像这样:

[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";
}

My output looks like this: 我的输出如下所示:

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

My class definition is obviously wrong. 我的班级定义显然是错误的。 Is there a way to achieve what I'm looking for using annotations? 有没有一种方法可以使用注解来实现所需的功能?

Just replace the attribute of Test1 with this: 只需将Test1的属性替换为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM