C# XML序列化指定节点名称

gooore 2016-07-27 05:47:31
您好,我在做XML序列化的时候有这么一段:

public List<XMLSystemChild> GetChildren
{
get { return m_oChildren; }
set { m_oChildren = value; }
}


List中的泛型XMLSystemChild是一个基类,被好多个派生类继承,这样我在输出XML序列化的时候遇到一个问题:

<XMLSystemChild xsi:type="XMLChild" ID="XMP_3" TagName="test5">
<Extent>
<Max X="1" Y="1" Z="1" />
</Extent>
<PersistentID />
<GenericAttributes Number="1" Set="test3">
<GenericAttribute Name="test2" Format="string" Value="2" />
</GenericAttributes>
<XMLSystemChild xsi:type="XMLChildEx" ID="XMP_1" TagName="test">
<Extent>
<Max X="1" Y="1" Z="1" />
</Extent>
<PersistentID />
<GenericAttributes Number="1" Set="test3">
<GenericAttribute Name="test2" Format="string" Value="2" />
</GenericAttributes>
</XMLSystemChild>
</XMLSystemChild>


节点中多 有个属性xsi:type="XMLChildEx" 说明派生类的类型。但是我不希望这样,希望能为每个派生类制定单独的节点名称。请问这个该如何做呢?

非常感谢您的解答!
...全文
579 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
linq to xml,读取成IEnumerable<XElement> 集合之后处理。
Forty2 2016-07-28
  • 打赏
  • 举报
回复
可以用XmlArrayItemAttribute:

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        My my = new My()
        {
            Name = "Minion",
            Pets = new List<Pet>() { new Dog(), new Cat() }
        };

        XmlSerializer serializer = new XmlSerializer(typeof(My));
        using (StringWriter sw = new StringWriter())
        {
            serializer.Serialize(sw, my);
            string xml = sw.ToString();
            /*
            <My xmlns:xsd="...">
              <Name>Minion</Name>
              <Pets>
                <Dog />
                <Cat />
              </Pets>
            </My>
            */
        }
    }
}

public class My
{
    public string Name { get; set; }
    [XmlArrayItem("Dog", Type = typeof(Dog))]
    [XmlArrayItem("Cat", Type = typeof(Cat))]
    public List<Pet> Pets { get; set; }
}
public class Pet { }
public class Dog : Pet { }
public class Cat : Pet { }
gooore 2016-07-28
  • 打赏
  • 举报
回复
谢谢您,我用XmlAttributeOverrides做好了

引用 1 楼 Forty2 的回复:
可以用XmlArrayItemAttribute:


using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

class Program
{
static void Main()
{
My my = new My()
{
Name = "Minion",
Pets = new List<Pet>() { new Dog(), new Cat() }
};

XmlSerializer serializer = new XmlSerializer(typeof(My));
using (StringWriter sw = new StringWriter())
{
serializer.Serialize(sw, my);
string xml = sw.ToString();
/*
<My xmlns:xsd="...">
<Name>Minion</Name>
<Pets>
<Dog />
<Cat />
</Pets>
</My>
*/
}
}
}

public class My
{
public string Name { get; set; }
[XmlArrayItem("Dog", Type = typeof(Dog))]
[XmlArrayItem("Cat", Type = typeof(Cat))]
public List<Pet> Pets { get; set; }
}
public class Pet { }
public class Dog : Pet { }
public class Cat : Pet { }

111,096

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧