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 开发者_StackOverflowwork?
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>
精彩评论