views:

1218

answers:

1

Hi,

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>

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

Thanks in advance.

+3  A: 

[XmlText], like so:

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" });
    }
}
Marc Gravell
Thanks a lot, just didn't find this XmlText :)
Haiko Wick