Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 序列化扩展列表的类的XML序列化程序_C#_Xml_Serialization - Fatal编程技术网

C# 序列化扩展列表的类的XML序列化程序

C# 序列化扩展列表的类的XML序列化程序,c#,xml,serialization,C#,Xml,Serialization,我有一个c#项目,其中包含接口和扩展该接口的类。显然,序列化程序不喜欢接口 所以,我必须创建一个扩展列表的集合类,这是可行的,但我需要排除列表根。请在下面找到代码示例 public interface ICreature [XmlRoot("Creature")] public class Man : ICreature [XmlRoot("Creature")] public class Alien : ICreature public class CreatureCollection

我有一个c#项目,其中包含接口和扩展该接口的类。显然,序列化程序不喜欢接口 所以,我必须创建一个扩展列表的集合类,这是可行的,但我需要排除列表根。请在下面找到代码示例

public interface ICreature

[XmlRoot("Creature")]
public class Man : ICreature

[XmlRoot("Creature")]
public class Alien : ICreature

public class CreatureCollection : List<ICreature>, IXmlSerializable
{
        public System.Xml.Schema.XmlSchema GetSchema() { return null; }

        public void ReadXml(XmlReader reader)
        {

        }

        public void WriteXml(XmlWriter writer)
        {
            foreach (ICreature aCreature in this)
            {
                XmlSerializer xmlSerializer = new XmlSerializer(aCreature.GetType());
                xmlSerializer.Serialize(writer, aCreature);
            }
        }
}

public class Something{
    ...
    public CreatureCollection{get;set;}
}


public void main(){
    Something sm = new Something();

    sm.CreatureCollection.Add(new Alien());
    sm.CreatureCollection.Add(new Man());
}
公共接口iFeature
[XmlRoot(“生物”)]
公共舱男:ICreature
[XmlRoot(“生物”)]
公共类外星人:ICreature
公共类CreatureCollection:列表,IXmlSerializable
{
public System.Xml.Schema.XmlSchema GetSchema(){return null;}
公共void ReadXml(XmlReader)
{
}
public void WriteXml(XmlWriter)
{
foreach(本节中的ICreature Acrature)
{
XmlSerializer XmlSerializer=新的XmlSerializer(aCreature.GetType());
serializer.Serialize(writer,aCreature);
}
}
}
公开课{
...
公共CreatureCollection{get;set;}
}
公共图书馆{
某物sm=新的某物();
sm.CreatureCollection.Add(new Alien());
sm.CreatureCollection.Add(newman());
}
XML输出:

<Something>
    <CreatureCollection>
        <Creature></Creature>
        <Creature></Creature>
    </CreatureCollection>
</Someting>

所需产出:

<Something>
        <Creature></Creature>
        <Creature></Creature>
</Someting>


请帮忙!。。。谢谢

像这样添加XmlElement

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

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            CreatureCollection creatures = new CreatureCollection() { creatures = new List<string>() { "", "", "" } };
            StringBuilder builder = new StringBuilder();
            StringWriter writer = new StringWriter(builder);
            XmlWriter xWriter = XmlWriter.Create(writer);
            creatures.WriteXml(xWriter);

        }
    }



    public class CreatureCollection : List<string>, IXmlSerializable
    {
        public List<string> creatures { get; set; }

        public System.Xml.Schema.XmlSchema GetSchema() { return null; }

        public void ReadXml(XmlReader reader)
        {

        }

        public void WriteXml(XmlWriter writer)
        {
            Something something = new Something() { creatures = new List<string>() };
            something.creatures.AddRange(creatures);

            XmlSerializer xmlSerializer = new XmlSerializer(typeof(Something));
            xmlSerializer.Serialize(writer, something);
        }
    }
    [XmlRoot("Something")]
    public class Something
    {
        [XmlElement("Creature")]
        public List<string> creatures { get; set; }
    }


}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
使用System.IO;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
CreatureCollection生物=新的CreatureCollection(){生物=新列表(){“”,“”};
StringBuilder=新的StringBuilder();
StringWriter编写器=新的StringWriter(生成器);
XmlWriter xWriter=XmlWriter.Create(writer);
WriteXml(xWriter);
}
}
公共类CreatureCollection:列表,IXmlSerializable
{
公共列表{get;set;}
public System.Xml.Schema.XmlSchema GetSchema(){return null;}
公共void ReadXml(XmlReader)
{
}
public void WriteXml(XmlWriter)
{
Something=newsomething(){biods=newlist()};
某物。生物。添加范围(生物);
XmlSerializer=newXmlSerializer(typeof(Something));
serializer.Serialize(writer,something);
}
}
[XmlRoot(“某物”)]
公开课
{
[XmlElement(“生物”)]
公共列表{get;set;}
}
}
​

我无法在没有CreatureCollection的情况下使用列表。默认xml序列化程序不喜欢序列化接口。它将抛出一个错误。我需要将CreatureCollection放在某个内容中,因为它应该是某个内容的一部分。而且,如果您在writexml函数中实例化某个东西,我不会在某个东西中丢失所有内容吗。假设某物具有其他属性。