开发者

Proper usage of XMLRootAttribute in Monotouch

I have a class called School that is serializable. When it serializes/deserializes I need the root element to be called school not School without having to change the class name to school. So I used the xmlroot attribute in the following way:

[XMLRoot(ElementName = "school")]

I also tried:

[XMLRoot("school")]

Neither of these did anything and the resulting XML file contained a root element call开发者_Go百科ed School.

Am I missing something?


I do not see what could be the problem but the following code works with MonoTouch 4 (maybe you'll find a difference between it and your own code).

I defined a class like:

[XmlRoot ("School")]
public class Wrong {
    public string Name { get; set; }
}

Then I serialized it to a MemoryStream which I then read into a string.

        Wrong bad = new Wrong ();
        XmlSerializer ser = new XmlSerializer(typeof(Wrong));
        using (MemoryStream ms = new MemoryStream ()) {
            ser.Serialize (ms, bad);
            ms.Position = 0;
            StreamReader sr = new StreamReader (ms);
            string st = sr.ReadToEnd ();
        }

The value of 'st' is:

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜