本文介绍了如何告诉 MessageQueue.SendMessageConnection 如何“XMLize"要发送的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 中使用 System.Messaging.MessageQueue.SendMessageConnection 来发送一个对象,其中包含一些参数,例如:

_sendQueue.Send(myObject, ...);

My myObject 是一个对象,包含一些属性,比如 Field1.

我正在检查我的消息是如何发送的,使用:

  • 计算机管理
  • 服务和应用
  • 消息队列
  • 私人队列
  • 打开正确的队列,然后在队列消息"中,右键单击并选中属性"、正文".

在那里我看到如下标签:

而不是这个,我想看到类似的东西:

我的对象中的属性和我想要使用的 XML 标签之间是否有简单的映射?

提前致谢

解决方案

这实际上很容易做到.查看 使用属性控制 XML 序列化 :

默认情况下,XML 元素名称由类或成员名称决定.在名为 Book 的简单类中,名为 ISBN 的字段将生成一个 XML 元素 tag ,如下例所示.

公开课书{公共字符串 ISBN;}//当 Book 类的实例被序列化时,它可能//生成此 XML://<ISBN>1234567890</ISBN>.

如果您想给元素一个新名称,可以更改此默认行为.以下代码显示了属性如何通过设置 XmlElementAttribute 的 ElementName 属性来启用此功能.

public class TaxRates {[XmlElement(ElementName = "TaxRate")]公共十进制 ReturnTaxRate;}

- Microsoft 文章截至 2017-03-30,不同作者(由我强调)

整篇文章大约需要 6 分钟阅读,我真的推荐它.

I'm working in C# with a System.Messaging.MessageQueue.SendMessageConnection for sending an object, containing some parameters, something like:

_sendQueue.Send(myObject, ...);

My myObject is an object, containing some attributes, like Field1.

I'm checking how my messages get sent, using:

  • Computer Management
  • Services and Applications
  • Message Queuing
  • Private Queues
  • open the right queue, and in the "queue messages", right-click and check "Properties", "Body".

There I see tags like:

<Field1>content_Field1</Field1>

Instead of this, I would like to see something like:

<F1>content_Field1</F1>

Is there an easy mapping between the attributes in my object and the XML tags I would like to be used?

Thanks in advance

解决方案

That's actually quite easy to do. Check out Control XML serialization using attributes :

public class Book
{
    public string ISBN;
}
// When an instance of the Book class is serialized, it might
// produce this XML:
// <ISBN>1234567890</ISBN>.
public class TaxRates {
    [XmlElement(ElementName = "TaxRate")]
    public decimal ReturnTaxRate;
}

- Microsoft article as of 2017-03-30, various authors (emphasis by me)

The whole article is about a ~6minutes read and I really recommend it.

这篇关于如何告诉 MessageQueue.SendMessageConnection 如何“XMLize"要发送的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-07 05:35