Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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# 如何使用XmlInclude动态指定类型?_C#_Xml_Object_Xmlinclude - Fatal编程技术网

C# 如何使用XmlInclude动态指定类型?

C# 如何使用XmlInclude动态指定类型?,c#,xml,object,xmlinclude,C#,Xml,Object,Xmlinclude,我使用的课程有: namespace defaultNamespace { ... public class DataModel { } public class Report01 {get; set;} public class Report02 {get; set;} } 我有一个方法可以创建下面的XML public XmlDocument ObjectToXml(object response, strin

我使用的课程有:

namespace defaultNamespace
{
    ...
    public class DataModel
    {
    }
    public class Report01
        {get; set;}
    public class Report02
        {get; set;}
}
我有一个方法可以创建下面的XML

public XmlDocument ObjectToXml(object response, string OutputPath)
{
    Type type = response.GetType();
    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
    serializer.Serialize(writer, response);
    XmlDocument xmldoc = new XmlDocument();
    stream.Position = 0;
    StreamReader sReader = new StreamReader(stream);
    xmldoc.Load(sReader);
    stream.Position = 0;
    string tmpPath = OutputPath;
    while (File.Exists(tmpPath))
    {
        File.Delete(tmpPath);
    }
    xmldoc.Save(tmpPath);
    return xmldoc;
}
我有两个列表,分别有Report01和Report02对象

List<object> objs = new List<object>();
List<object> objs2 = new List<object>();

Report01 obj = new Report01();
obj.prop1 = "aa";
obj.prop2 = "bb";
objs.Add(obj);

Report02 obj2 = new Report02();
obj2.prop1 = "cc";
obj2.prop2 = "dd";
objs2.Add(obj2);
我看到这个例外:

[XmlInclude(typeof(Report01)]
[XmlInclude(typeof(Report02)]
public class BaseClass { }
public class Report01 : BaseClass { ... }
public class Report02 : BaseClass { ... }

List<BaseClass> objs = new List<BaseClass>();
List<BaseClass> objs2 = new List<BaseClass>();
// fill collections here
ObjectToXml(objs, "c:\\12\\objs.xml");
ObjectToXml(objs2, "c:\\12\\objs2.xml");
不应使用类型“Report01(或Report02)”。使用xmlclude 或SoapInclude属性指定未知的类型 静态地


如何解决此问题?

这是因为您的
响应。GetType()
实际上返回
列表
类型,然后您尝试序列化非预期类型<代码>对象不了解您的类型,
对象的序列化程序
无法序列化未知类型

您可以将基类用于报告和
xmlclude
来解决此异常:

[XmlInclude(typeof(Report01)]
[XmlInclude(typeof(Report02)]
public class BaseClass { }
public class Report01 : BaseClass { ... }
public class Report02 : BaseClass { ... }

List<BaseClass> objs = new List<BaseClass>();
List<BaseClass> objs2 = new List<BaseClass>();
// fill collections here
ObjectToXml(objs, "c:\\12\\objs.xml");
ObjectToXml(objs2, "c:\\12\\objs2.xml");
[xmlclude(typeof(Report01)]
[xmlclude(typeof(Report02)]
公共类基类{}
公共类Report01:基类{…}
公共类报表02:基类{…}
List objs=new List();
List objs2=新列表();
//在这里填写集合
ObjectToXml(objs,“c:\\12\\objs.xml”);
ObjectToXml(objs2,“c:\\12\\objs2.xml”);

所以,您说我需要创建一个Report01和Report02将继承的基类。对吗?是的,因为您不能向
对象添加
xmlcludeAttribute
。或者您可以声明
List
List
,而不是
List