THis is test for 页首

MSMQ 中XmlMessageFormatter 指定可序列化的属性

前言

  这是一篇关于使用 MS MessageQueue  微软消息队列组件的 麦库笔记 现在搬运到博客园上来

在使用MSMQ 发送消息时 抛出:

IContlliton<T> 是接口 无法序列化的错误。

需要发送的消息是复杂实体:

public class modelOne
{

   public string name{get;set;}

    public ModelTwo model{get;set;}

}

public class ModelTwo

{

    public string name{get;set;}

    public IList<ModleThree> list{get;set;}

}

 

由于其中的ModelTwo 里有接口无法进行序列化。但是list 其实是不需要序列化的。

为了指定需要序列化的属性 可以利用       [XmlIgnore] 标签。该标签表示不需要序列化的属性字段。

 

更多关于XML 序列化时的attriabute 有:

System.Xml.Serialization命名空间中有一系列的特性类,用来控制复杂类型序列化的控制。例如 XmlElementAttribute、XmlAttributeAttribute、XmlArrayAttribute、 XmlArrayItemAttribute、XmlRootAttribute等等

 

 [XmlRoot("cat")]
    public class Cat
    {
        //定义Color属性的序列化为cat节点的属性
        [XmlAttribute("color")]
        public string Color { get; set; }

        //要求不序列化Speed属性
        [XmlIgnore]
        public int Speed { get; set; }

        //设置Saying属性序列化为Xml子元素
        [XmlElement("saying")]
        public string Saying { get; set; }
    }
}

可以使用XmlElement指定属性序列化为子节点(默认情况会序列化为子节点);或者使用XmlAttribute特性制定属性序列化为Xml节点的属性;还可以通过XmlIgnore特性修饰要求序列化程序不序列化修饰属性

 参考链接

http://www.cnblogs.com/luomingui/archive/2010/03/08/1680933.html

 

posted @ 2012-08-01 19:30  DotDot  阅读(1611)  评论(0编辑  收藏  举报
页脚