Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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# XMLSerialization:参数对象的类型';西南和西南地区;它不是原始的_C#_Xml Serialization_Primitive_Derived Class - Fatal编程技术网

C# XMLSerialization:参数对象的类型';西南和西南地区;它不是原始的

C# XMLSerialization:参数对象的类型';西南和西南地区;它不是原始的,c#,xml-serialization,primitive,derived-class,C#,Xml Serialization,Primitive,Derived Class,我试图将一个对象序列化为一个XML文件,但遇到上述错误 问题似乎在于包含基类列表但由基类派生的对象填充的对象 示例代码如下所示: public class myObject { public myObject() { this.list.Add(new Sw()); } public List<Units> list = new List<Units>(); } public class Units { publ

我试图将一个对象序列化为一个XML文件,但遇到上述错误

问题似乎在于包含基类列表但由基类派生的对象填充的对象

示例代码如下所示:

public class myObject
{
    public myObject()
    {
        this.list.Add(new Sw());
    }

    public List<Units> list = new List<Units>();
}

public class Units
{
    public Units()
    {
    }
}

public class Sw : Units
{
    public Sw();
    {
    }

    public void main()
    {
        myObject myObject = new myObject();
        XmlSerializer serializer = new XmlSerializer(typeof(myObject));
        TextWriter textWriter = new StreamWriter ("file.xml");
        serializer.Serialize (textWriter, myObject);
    }
公共类myObject
{
公共对象()
{
this.list.Add(新的Sw());
}
公共列表=新列表();
}
公营单位
{
公共单位()
{
}
}
公共类软件:单位
{
公共软件();
{
}
公共图书馆
{
myObject myObject=新的myObject();
XmlSerializer serializer=新的XmlSerializer(typeof(myObject));
TextWriter TextWriter=newstreamwriter(“file.xml”);
序列化程序。序列化(textWriter,myObject);
}
例如,仅包含由继承自
单元
类(
Sw
)的派生对象填充的
列表的对象

很抱歉没有提供我的实际代码,但是所涉及的对象非常复杂,这似乎是对象中唯一无法成功序列化的部分——并且只有在列表包含派生类时


如何正确序列化这样的类?

使用
xmlclude
属性标记
Units
类,并将派生类作为参数传递:

[XmlInclude(typeof(Sw))]
public class Units
{
    public Units()
    {
    }
}

将对象序列化为XML

public String SerializeResponse(SW sw)
{
    try
    {
        String XmlizedString = null;
        XmlSerializer xs = new XmlSerializer(typeof(SW));
        //create an instance of the MemoryStream class since we intend to keep the XML string 
        //in memory instead of saving it to a file.
        MemoryStream memoryStream = new MemoryStream();
        //XmlTextWriter - fast, non-cached, forward-only way of generating streams or files 
        //containing XML data
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        //Serialize emp in the xmlTextWriter
        xs.Serialize(xmlTextWriter, sw);
        //Get the BaseStream of the xmlTextWriter in the Memory Stream
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        //Convert to array
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
    }
    catch (Exception ex)
    {
        throw;
    }
}
该方法将返回一个XML字符串,要生成上述函数,您需要导入以下库:

using System.Xml;
using System.Xml.Serialization;
using System.IO;

对于我认为是一个简单的问题,很难找到一个简单的答案。我的代码现在可以工作了,谢谢!我很感谢提供的答案,但这不是我真正想要的。对于任何混淆,我深表歉意。