CaffGeek
CaffGeek

Reputation: 22064

Xml Serialization Attribute and Text

I have a simple Xml Node that I need to recreate

<Division ID="123">Division Name</Division> 

But when I create the class as

public class Division
{
    [XmlAttribute]
    public string Id { get; set; }

    [XmlText]
    public string Description { get; set; }
}

I get

<Division>Division Name</Division> 

The Id vanishes.

How can I make this work?

Upvotes: 0

Views: 93

Answers (1)

Nix
Nix

Reputation: 58572

Works on my machine


Division d = new Division()
{
    Id = "1",
    Description = "Description"
};

FileStream fs = new FileStream("test.txt", FileMode.Create);
TextWriter writer = new StreamWriter(fs, new UTF8Encoding());


XmlSerializer xs = new XmlSerializer(typeof(Division));
xs.Serialize(writer, d);
writer.Close();

<?xml version="1.0" encoding="utf-8"?>
<Division xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
       xmlns:xsd="http://www.w3.org/2001/XMLSchema" Id="1">Description</Division>

Upvotes: 1

Related Questions