Exclude root node from xml using xmlserializer

不想你离开。 提交于 2019-12-11 06:44:29

问题


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>

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.


回答1:


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
        }

}


来源:https://stackoverflow.com/questions/48440400/exclude-root-node-from-xml-using-xmlserializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!