使用通用类型的XmlElement名称进行Xml序列化 [英] Xml Serialization using the XmlElement name of the generic type

查看:67
本文介绍了使用通用类型的XmlElement名称进行Xml序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个将用于XML序列化的通用类.我用来保存通用对象T的属性称为消息",但我希望将此属性创建的XML节点命名为与在ElementType类上标记的名称相同.

I'm trying to create a generic class that i will use for XML Serialization. The property I use to hold the generic object T is called 'Message' but I want the created XML Node of this property to be named as i notated on the class ElementType.

示例:

public class MessageWrapper<T>where T : class
{
    [XmlElement]
    public T Message { get; set; }
}

我想保留在Message属性上的对象类-

[XmlType("Connect"), Serializable]
public class ConnectMessage
{
    [XmlElement("Machine_Name")]
    public string MachineName { get; set; }

    [XmlElement("Application_Name")]
    public string AppName { get; set; }
}

现在的输出XML是这个-

<Message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Message_ID>1</Message_ID>
  <Message>
    <Machine_Name>WS-8193</Machine_Name>
    <Application_Name>TestApplication</Application_Name>
  </Message>
</Message>

代替:

<Message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Message_ID>1</Message_ID>
  <Connect>
    <Machine_Name>WS-8193</Machine_Name>
    <Application_Name>TestApplication</Application_Name>
  </Connect>
</Message>

有可能吗?


Is that possible?



推荐答案

元素名称来自属性,而不是基础类型.如果要将Message属性称为Connect,则必须将其放入属性的XmlElement中.串行器通过反射进行工作.为了序列化 对象,最后将类型指定为MessageWrapper< ConnectMessage>.因此,序列化程序知道类型为ConnectMessage,但元素名称为Message.对于反序列化,它将完全相反.它会看到Message元素 并将其映射到属性.由于该属性将是将创建的对象ConnectMessage类型.

The element name comes from the property, not the underlying type. If you want the Message property to be called Connect then that is what you'd have to put in the XmlElement of the property. The serializer works by reflection. In order to serialize the object you'll end up specifying the type as MessageWrapper<ConnectMessage>. As such the serializer knows the type is ConnectMessage but the element name is Message. For deserialization it is going to do the exact opposite. It'll see the Message element and map it to the property. Since the property will be of type ConnectMessage that is the object it'll create.

不太确定为什么要尝试将元素名称映射到类型,但这不是XML序列化的工作原理.如果要执行类似的操作,则需要使用XAML或等效方法而不是XML.替代方法是手动生成XML 或创建自定义序列化程序.但是,如果您的目标是创建一个通用的序列化器/反序列化器,使您可以传递任何类型并可以在以后对它进行反序列化,那么它将无法正常工作,因为序列化器需要具体的类型才能进行反射 反对.

Not really sure why you're trying to map element names to types but that isn't how XML serialization works. If you want to do something like that then you'll want to use XAML or equivalent rather than XML. The alternative would be generate the XML by hand or create a custom serializer. But if your goal is to create a generalized serializer/deserializer that allows you to pass any type and be able to deserialize it later then it isn't going to work as the serializer requires a concrete type in order to reflection against it.

迈克尔·泰勒
http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于使用通用类型的XmlElement名称进行Xml序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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