本文介绍了使用 xmlserializer 从 xml 中排除根节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套类,我使用 xmlserializer 将其转换为 xml.

I have a nested class which i use xmlserializer to convert it to xml.

public class RequestModel{

        [XmlElement("message", Namespace = "http://www.origostandards.com/schema/mtg/v2")]
        public Message message { get; set; }

        public RequestModel()
        {
            this.message = new Message();
        }
        public class Message
        {
           //other constructor here etc
        }
}

当它序列化时,所有编译都没有问题,但输出如下:

When it serializes it all compiles without issue however the output is as follows:

<RequestModel>
   <mtg:message>
   ...
   </mtg:message>
</RequestModel>

有没有办法从序列化中排除类名,以便消息成为顶级节点并且输出看起来像:

Is there a way to exclude the class name from serializing so that message would become the top node and the output would look like:

<mtg:message>
  ...
</mtg:message>

我尝试添加一个布尔值来显示节点以及 XmlIgnore 和 XmlRoot 等内容,但这些属性并不适合我的解决方案.

I have tried adding a Boolean for visibility of the node as well as things as XmlIgnore and XmlRoot but these attributes don't really fit with my solution.

这里的任何帮助将不胜感激.

Any help here would be appreciated.

推荐答案

尝试不同的 c# 命名空间 :

Try different c# namespaces :

namespace RequestA
{
    public class RequestModel
    {

        [XmlElement("message", Namespace = "http://www.origostandards.com/schema/mtg/v2")]
        public Message.Message message { get; set; }

        public RequestModel()
        {
            this.message = new Message.Message();
        }
     }
}
namespace RequestB
{

    public class RequestModel
    {
        [XmlElement("message", Namespace = "http://www.origostandards.com/schema/mtg/v2")]
        public Message.Message message { get; set; }

        public RequestModel()
        {
            this.message = new Message.Message();
        }
    }
}
namespace Message
{
        public class Message
        {
            //other constructor here etc
        }

}

这篇关于使用 xmlserializer 从 xml 中排除根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 18:14