简体   繁体   English

使用xmlserializer从xml排除根节点

[英]Exclude root node from xml using xmlserializer

I have a nested class which i use xmlserializer to convert it to xml. 我有一个嵌套类,我使用xmlserializer将其转换为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>

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. 我尝试添加一个布尔值来查看节点以及XmlIgnore和XmlRoot的可见性,但是这些属性实际上并不适合我的解决方案。

Any help here would be appreciated. 在这里的任何帮助,将不胜感激。

Try different c# namespaces : 尝试不同的c#名称空间:

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
        }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM