XML序列化的问题 - 如何序列化元素,属性和文本从一个对象

问题描述:

我是新进使用.NET XML序列化和与它一起工作了一段时间,我现在挺后fuzzled。我可以用序列化属性包含其他元素的元素,但我怎么能序列类似

I'm new into XML Serialization using .NET and after working with it for some time I'm quite fuzzled now. I can serialize elements with attributes containing other elements but how can I serialize something like

<myElement name="foo">bar</myElement>

我用myElement一类与XmlAttribute为名,而是如何引用XML元素的价值?

I use a class for myElement with a XmlAttribute for the "name", but how to refer the value of the XML Element?

在此先感谢。

[XmlText]$c$c>,像这样:

using System;
using System.Xml.Serialization;
[Serializable, XmlRoot("myElement")]
public class MyType {
    [XmlAttribute("name")]
    public string Name {get;set;}

    [XmlText]
    public string Text {get;set;}
} 
static class Program {
    static void Main() {
        new XmlSerializer(typeof(MyType)).Serialize(Console.Out,
            new MyType { Name = "foo", Text = "bar" });
    }
}