XmlSerializer Serialize an empty variable to use both tags?

I want to load a serialized xml class in Soap Envelope. I am starting, so I am not filling in the insides, so it looks like this:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" /> 

I want it to look like this:

 <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" ></Envelope>` 


The class I wrote is as follows:

 [System.SerializableAttribute()] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")] [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/",ElementName="Envelope", IsNullable = true)] public class TestXmlEnvelope { [System.Xml.Serialization.XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")] public System.Collections.ArrayList Body = new System.Collections.ArrayList(); } //class TestXmlEnvelope` 

I use this as an example, as other people may want it in a separate element. I am sure it should be simple, but unfortunately I do not know a suitable keyword for this.

Thanks as always for your help.

[Edit] Error while trying to use this instruction

 System.Xml.Serialization.XmlSerializer xmlout = new System.Xml.Serialization.XmlSerializer(typeof(TestXmlEnvelope)); System.IO.MemoryStream memOut = new System.IO.MemoryStream(); xmlout.Serialize(memOut, envelope, namespc); Microsoft.Web.Services.SoapEnvelope soapEnv = new Microsoft.Web.Services.SoapEnvelope(); soapEnv.Load(memOut); 

This gives me the error "Root Element not found".

[Edit] I fixed the error, the problem was that after I serialized the object, I did not set memOut.Position = 0. However, I hope this question helps other people who want to do this.

+4
source share
3 answers

The main problem here is that the XmlSerializer calls WriteEndElement() in the XmlWriter when it will write the end tag. This, however, generates an abbreviated form <tag/> when there is no content. WriteFullEndElement() writes the end tag separately.

You can enter your own XmlTextWriter in the middle, which the serializer will then use to demonstrate this functionality.

Given that serializer is a suitable XmlSerializer , try the following:

 public class XmlTextWriterFull : XmlTextWriter { public XmlTextWriterFull(TextWriter sink) : base(sink) { } public override void WriteEndElement() { base.WriteFullEndElement(); } } ... var writer = new XmlTextWriterFull(innerwriter); serializer.Serialize(writer, obj); 

[Change] for the case of your added code, add facade designers for:

 public XmlTextWriterFull(Stream stream, Encoding enc) : base(stream, enc) { } public XmlTextWriterFull(String str, Encoding enc) : base(str, enc) { } 

Then use the memory stream as your internal thread in the constructor, as before:

 System.IO.MemoryStream memOut = new System.IO.MemoryStream(); XmlTextWriterFull writer = new XmlTextWriterFull(memOut, Encoding.UTF8Encoding); //Or the encoding of your choice xmlout.Serialize(writer, envelope, namespc); 
+10
source

Note to write: OP used *** Microsoft. *** Web.Services.SoapEnvelope class, which is part of the extremely obsolete WSE 1.0 product. This class is derived from the XmlDocument class, so it is possible that the same problems were noticed with XmlDocument.

In no case should you use WSE for any new development, and if it is already in use, the code should be copied as soon as possible. WCF or ASP.NET Web APIs are the only technologies that should be used for .NET web services in the future.

+1
source

Two representations are equivalent. Why do you need to appear in the last form?

-2
source

All Articles