How to enable attributes for an element (not the root) with XML serialization in C #

I need to create an XML that looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <inboundMessage xmlns="http://www.myurl.net"> <header> <password>mypwd</password> <subscriberId>myuser</subscriberId> </header> <message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="myType"> <eventDate>2012-09-05T12:13:45.561-05:00</eventDate> <externalEventId /> <externalId>SomeIdC</externalId> </message> </inboundMessage> 

The problem is that I do not know how to include xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: type = "myType" in my tag. My class that I need to serialize is as follows:

 [XmlType("inboundMessage")] [XmlRoot(Namespace = "http://www.myurl.net")] public class InboundMessage { [XmlElement(ElementName = "header")] public Header _header; [XmlElement(ElementName = "message")] public List<MyType> _messages; } 

What XmlAttributes do I need to add to my _messages member to serialize it the way I want?

TIA, Ed

+4
source share
2 answers

Use XmlAttribute as follows:

 public class MyType { [XmlAttribute("type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] public string Type { get; set; } } 
+1
source

A somewhat similar option appeared in my team, but more complete. Two properties have been added to MyType:

  [XmlNamespaceDeclarations] public XmlSerializerNamespaces Namespaces { get; set; } [XmlAttribute("type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")] public string Type { get { return _type; } set { _type = value; } } 

with _type is defined as follows:

  private string _type = "myType"; 

Then serialization was done differently:

  // declare an XmlAttributes object, indicating that the namespaces declaration should be kept var atts = new XmlAttributes { Xmlns = true }; // declare an XmlAttributesOverrides object and ... var xover = new XmlAttributeOverrides(); // ... add the XmlAttributes object with regard to the "Namespaces" property member of the "Message" type xover.Add(typeof(MyType), "Namespaces", atts); // set the Namespaces property for each message in the payload body var messageNamespaces = new XmlSerializerNamespaces(); messageNamespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); foreach (var message in myInboundMessage._messages) { message.Namespaces = messageNamespaces; } // create a serializer var serializer = new XmlSerializer(object2Serialize.GetType(), xover); // add the namespaces for the root element var rootNamespaces = new XmlSerializerNamespaces(); rootNamespaces.Add("", "http://www.myurl.net"); // serialize and extract the XML as text string objectAsXmlText; using (var stream = new MemoryStream()) using (var xmlTextWriter = new XmlTextWriter(stream, null)) { serializer.Serialize(xmlTextWriter, object2Serialize, rootNamespaces); stream.Seek(0, SeekOrigin.Begin); var buffer = new byte[stream.Length]; stream.Read(buffer, 0, (int)stream.Length); objectAsXmlText = Encoding.UTF8.GetString(buffer); } 

Finally, InboundMessage was decorated as follows:

 [XmlRoot("inboundMessage", Namespace = "http://www.myurl.net")] 

With this approach, I get everything I need.

0
source

Source: https://habr.com/ru/post/1432771/


All Articles