开发者

Xml Serialization Attribute and Text

开发者 https://www.devze.com 2023-01-21 01:07 出处:网络
I have a simple Xml Node that I need to recreate <Division ID=\"123\">Division Name</Division>

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?


Xml Serialization Attribute and Text


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

精彩评论

暂无评论...
验证码 换一张
取 消