How to convert Raw XML to SOAP XML in C#?
I have some xml 开发者_JAVA百科generated from the XML Serializer ..How can I convert it to SOAP XML ?...I am trying to do it ASP.NET C#...please help me out
You will just need to create a data class that can be serialized by both the XMLSerializer and the SOAPFormatter. This likely means you will need a public class with public properties for the XMLSerializer and you will need to add the Serializable attribute for the SOAPFormatter. Otherwise, it is pretty straight forward.
I created a Naive example to illustrate what I mean:
[Serializable]
public class MyData
{
public int MyNumber { get; set; }
public string Name { get; set; }
}
class Program
{
static void Main(string[] args)
{
using (MemoryStream stream = new MemoryStream())
{
MyData data = new MyData() { MyNumber = 11, Name = "StackOverflow" };
XmlSerializer serializerXML = new XmlSerializer(data.GetType());
serializerXML.Serialize(stream, data);
stream.Seek(0, SeekOrigin.Begin);
data = (MyData)serializerXML.Deserialize(stream);
// We're cheating here, because I assume the SOAP data
// will be larger than the previous stream.
stream.Seek(0, SeekOrigin.Begin);
SoapFormatter serializerSoap = new SoapFormatter();
serializerSoap.Serialize(stream, data);
stream.Seek(0, SeekOrigin.Begin);
data = (MyData)serializerSoap.Deserialize(stream);
}
}
}
There's no such thing as "raw XML" and "SOAP XML".
What are you trying to accomplish? If you're just trying to return XML as a response from a web service, then just get it into an XmlDocument or XDocument, and just return the root element:
[WebMethod]
public XmlElement ReturnXml()
{
XmlDocument doc = new XmlDocument();
doc.Load(fromSomewhere);
return doc.DocumentElement;
}
it sounds like you want to wrap your xml into a soap envelop ? if so try this
精彩评论