简体   繁体   English

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

[英]How to use XmlInclude to specify types dynamically?

I have these classes which I use: 我有以下这些类:

namespace defaultNamespace
{
    ...
    public class DataModel
    {
    }
    public class Report01
        {get; set;}
    public class Report02
        {get; set;}
}

And I have a method that creates the XML below. 我有一个下面创建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;
}

And I have two lists that has a Report01 and Report02 object. 我有两个具有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);

When I try to create the XML like this: 当我尝试像这样创建XML时:

ObjectToXml(objs, "c:\\12\\objs.xml");
ObjectToXml(objs2, "c:\\12\\objs2.xml");

I see this exception: 我看到此异常:

The type "Report01(or Report02)" was not expected. 不应使用类型“ Report01(或Report02)”。 Use the XmlInclude or SoapInclude attribute to specify types that are not known statically. 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。

How can I solve this problem? 我怎么解决这个问题?

It's because your response.GetType() actually returns List<object> type and then you tried to serialize non expected type. 这是因为您的response.GetType()实际上返回List<object>类型,然后尝试序列化非预期类型。 Object don't know anything about your types and serializer for Object can't serialize your unknown types. Object对您的类型一无所知,而Object序列化程序无法对未知类型进行序列化。

You can use BaseClass for your reports and XmlInclude to solve this exception: 您可以将BaseClass用于报告,而XmlInclude可以解决此异常:

[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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型 - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically 如何动态添加XmlInclude属性 - How to add XmlInclude attribute dynamically “不希望使用RoleProxy类型。 使用XmlInclude或SoapInclude属性可以指定静态未知的类型。” NHibernate - “The type RoleProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.” NHibernate xxx类型不是预期的。 使用XmlInclude或SoapInclude属性来指定静态未知的类型 - The type xxx was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically ASMX 服务错误 - 使用 XmlInclude 或 SoapInclude 属性指定静态未知的类型 - ASMX Service Error - Use the XmlInclude or SoapInclude attribute to specify types that are not known statically 如何使用XmlInclude序列化IEnumerable - How to use XmlInclude to serialize IEnumerable 序列化成员类型而不使用.NET中的XmlInclude - Serialize member types without using XmlInclude in .NET XmlInclude在单独的程序集中对(继承)序列化继承的类型 - XmlInclude to (De)Serialize Inherited Types in Separate Assemblies 类型xxxx不期望使用xmlinclude或soapinclude - type xxxx not expected use xmlinclude or soapinclude WCF服务引发“未预期类型[…]。 即使已经使用XmlInclude,也要使用XmlInclude […]” - WCF service throws “The type […] was not expected. Use the XmlInclude […]” even if XmlInclude is already being used
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM