Alex T
Alex T

Reputation: 765

XML Attributes of C# Class - attribute and value

I am trying to serialize a C# class into the following:

<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?

Upvotes: 0

Views: 276

Answers (1)

Xiaoy312
Xiaoy312

Reputation: 14477

Just replace the attribute of Test1 with this:

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

Upvotes: 2

Related Questions