XmlSerializer的列表项元素名称 [英] XmlSerializer List Item Element Name

查看:136
本文介绍了XmlSerializer的列表项元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类 PersonList

[XmlRoot("Persons")]
PersonList : List<Human>

在我这个序列化到XML,默认情况下它会产生这样的:

when I serialize this to XML, by default it will produce something like this:

<Persons>
  <Human>...</Human>
  <Human>...</Human>
</Persons>

我的问题是需要的东西以改变元素做输出?所以输出是:

<Persons>
  <Person>...</Person>
  <Person>...</Person>
</Persons>

和,如何反序列化上面的XML到 PersonList 类对象?

and, how to deserialize the above XML to the PersonList class object?

每尼克的建议,这是我的测试code:

Per Nick's advice, Here is my testing code:

[XmlRoot("Persons")]
public class Persons : List<Human>
{

}

[XmlRoot("Person")]
public class Human
{
    public Human()
    {
    }

    public Human(string name)
    {
        Name = name;
    }

    [XmlElement("Name")]
    public string Name { get; set; }

}

void TestXmlSerialize()
{
    Persons personList = new Persons();
    personList.Add(new Human("John"));
    personList.Add(new Human("Peter"));

    try
    {
        using (StringWriter writer = new StringWriter())
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Persons));
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;

            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, string.Empty);

            XmlWriter xmlWriter = XmlWriter.Create(writer, settings);
            serializer.Serialize(xmlWriter, personList, namespaces);

            Console.Out.WriteLine(writer.ToString());
        }
    }
    catch (Exception e)
    {
        Console.Out.WriteLine( e.ToString());
    }
}

测试code的输出是:

The output of the testing code is:

<Persons>
  <Human>
    <Name>John</Name>
  </Human>
  <Human>
    <Name>Peter</Name>
  </Human>
</Persons>

随着输出显​​示, [XmlRoot(人)] 不更改标记到

推荐答案

我不认为有是你控制生成的数组元素的名称的方法。

I don't think there is a way for you to control the name of the generated array elements.

如果你能然而包住另一个类的内部人士集合然后你会拥有比使用生成的输出完全控制 XmlArrayAttribute XmlArrayItemAttribute

If you can however wrap the Persons collection inside another class you will then have complete control over the generated output using XmlArrayAttribute and XmlArrayItemAttribute.

如果您不能创建新类,你可以诉诸实施的IXmlSerializable ,但是这要复杂得多。

If you cannot create this new class you can resort to implementing IXmlSerializable, but this is much more complex.

对于第一种选择示例如下:

An example for the first alternative follows:

[XmlRoot("Context")]
public class Context
{
    public Context() { this.Persons = new Persons(); }

    [XmlArray("Persons")]
    [XmlArrayItem("Person")]
    public Persons Persons { get; set; }
}

public class Persons : List<Human> { }

public class Human
{
    public Human() { }
    public Human(string name) { Name = name; }
    public string Name { get; set; }
}

class Program
{
    public static void Main(string[] args)
    {
        Context ctx = new Context();
        ctx.Persons.Add(new Human("john"));
        ctx.Persons.Add(new Human("jane"));

        var writer = new StringWriter();
        new XmlSerializer(typeof(Context)).Serialize(writer, ctx);

        Console.WriteLine(writer.ToString());
    }
}

这篇关于XmlSerializer的列表项元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆