在派生类中使用C#XmlSerializer的同时控制生成的XML元素 [英] Using C# xmlserializer on derived classes while controlling the generated XML element

查看:262
本文介绍了在派生类中使用C#XmlSerializer的同时控制生成的XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有类与抽象基类,以及各种类从这个抽象类派生的C#结构。

  [System.Xml.Serialization.XmlInclude(typeof运算(b))] 
公共抽象类A {

}

[Serializable接口]
〔XmlType将(类型名称)]
公共b类:A {

}

此外,我使用中的另一个C类b类的实例作为其抽象类型是这样一个领域:

 公共C类{
...
公开发行A myItem {搞定;组; } //一个实际上是B型

}



的现在,当我通过标准的XmlSerializer序列化我的C类,我得到一个XML结构是这样的:

 < C> 
< myItem P2:TYPE =类型名称的xmlns:P2 =...>
... //领域的元素和材料
< / myItem>
< / C>



不过那不是我所需要的,因为我把那些序列化C对象为REST web服务(没有有效的模式还)。我真正需要的是这样的:

 < C> 
< typename的>
... //领域的元素和材料
< /类型名称>
< / C>



但是,正如你可以在上面看到,XmlSerializer的似乎过的类型名称设置为喜欢的实例字段名称通过XmlType将。此外,很明显,我不能只用的XmlElement(类型名),我在C字段,因为我不知道我的抽象类A的具体实施领域将包含



有没有人有类似的问题,并能为我提供一个解决的办法? ?或者我真的需要在我所有的混凝土派生类实现IXmlSerializable的在我的A类,从而得到这个工作。



编辑:刚刚发现在阅读一些文章,IXmlSerializable的并不让我控制的包装元素,所以我是不是真的需要实现在C类接口带有某种对myItem成员的类型开关()的?



感谢您的帮助!



最好的问候,
FLO


< DIV CLASS =h2_lin>解决方案

好吧,看来我已经找到了解决办法,至少系列化,还不知道这是否也适用于反序列化。关键是对于XMLELEMENT属性,允许指定要使用的密钥,如果一个抽象类型使用了特殊的构造:



http://msdn.microsoft.com/en-us/library/cz6bdh5z.aspx



也许这也有利于别人。



最佳,
FLO


I've got a C# structure of classes with an abstract base class and various classes derived from this abstract class.

[System.Xml.Serialization.XmlInclude(typeof(B))]
public abstract class A {
 ...
}

[Serializable]
[XmlType("typename")]
public class B : A  {
 ...
}

Furthermore, I'm using an instance of class B within another class C as a field with its abstract type like this:

public class C {
    ...
    public A myItem { get; set; } //A is actually of type B
    ...
}

Now, when I serialize my class C via the standard xmlserializer, I get a XML structure like this:

<C>
    <myItem p2:type="typename" xmlns:p2="...">
     ... //Fields as elements and stuff
    </myItem>
</C>

But thats not what I need because I send those serialized C objects to a REST Webservice (which has no valid schema yet). What I actually need is something like this:

<C>
    <typename>
     ... //Fields as elements and stuff
    </typename>
</C>

But as you can see above, the xmlserializer seems to prefer the instance field name over the type name set via XmlType. Also, obviously I can't just use XmlElement("typename") for my field in C, because I don't know which concrete implementation of my abstract class A the field will contain.

Has anyone ever had a similar problem and could provide me with a solution to this? Or do i really need to implement IXmlSerializable in my class A and thus within all of my concrete A-derived classes to get this working?

EDIT: Just found out while reading some articles that IXmlSerializable doesn't let me control the wrapper element, so do I actually need to implement the Interface in class C with some sort of switch() on the type of the myItem member?

Thanks for your help!

Best regards, flo

解决方案

Okay, seems I have found the solution, at least for serialization, don't know yet if this also works for deserialization. The key is a special constructor for the XMLElement attribute, which allows to specify the key to use if a abstract type is used:

http://msdn.microsoft.com/en-us/library/cz6bdh5z.aspx

Maybe this is also helpful to someone else.

Best, flo

这篇关于在派生类中使用C#XmlSerializer的同时控制生成的XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆