I am working on create XML string through Class object in C#. I am trying to add namespace in child element, but it's added in parent element of XML̥ file.
I have tried below code:
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class EnvelopeObj
{
[XmlElement(ElementName = "Header", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Header Header { get; set; } = new();
[XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Body Body { get; set; } = new();
}
public class Header { }
public class Body
{
[XmlElement(ElementName = "StoreOrderRequest", Namespace = "http://test.com/wsdl/OrderingMobility/v8_0")]
public StoreOrderRequestMain StoreOrderRequest { get; set; } = new();
}
[XmlRoot(ElementName = "StoreOrderRequest", Namespace = "http://test.com/wsdl/OrderingMobility/v8_0")]
public class StoreOrderRequestMain
{
[XmlElement(ElementName = "MessageHeader", Namespace = "http://test.com/schema/MsgHeader/v1_0")]
public MessageHeader MessageHeader { get; set; } = new();
[XmlElement(ElementName = "StoreOrderRequest", Namespace = "")]
public StoreOrderRequestObj StoreOrderRequest { get; set; } = new();
}
public class StoreOrderRequestObj
{
public string AccountGroupId { get; set; }
}
[XmlRoot(ElementName = "MessageHeader", Namespace = "http:/test.com/schema/MsgHeader/v1_0")]
public class MessageHeader
{
public string AgreementId { get; set; }
}
Please look in below code to convert xml string:
EnvelopeObj envelope = new ();
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("", "http://test.att.com/schema/OrderingMobility/v8_0");
namespaces.Add("SOAPENC", "http://schemas.xmlsoap.org/soap/encoding/");
namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema");
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
namespaces.Add("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.Add("m", "http://test.com/wsdl/OrderingMobility/v8_0");
// Create an XmlSerializer for the MyClass type
XmlSerializer serializer = new XmlSerializer(typeof(EnvelopeObj));
Encoding encoding = Encoding.UTF8;
string xmlString = string.Empty;
using (MemoryStream memoryStream = new MemoryStream())
{
// Create an XmlWriter with the specified encoding
using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, new XmlWriterSettings { Encoding = encoding,Indent=true, OmitXmlDeclaration = true }))
{
// Serialize the object
serializer.Serialize(xmlWriter, envelope,namespaces);
}
// Convert the MemoryStream to a byte array
byte[] xmlBytes = memoryStream.ToArray();
// Convert the byte array to a string using the specified encoding
xmlString = encoding.GetString(xmlBytes);
// Print the XML string
Console.WriteLine(xmlString);
}
With above code I am getting below XML string:
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope
xmlns="http://test.com/schema/OrderingMobility/v8_0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:m="http://test.com/wsdl/OrderingMobility/v8_0"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<m:StoreOrderRequest>
<MessageHeader xmlns="http://test.com/schema/MsgHeader/v1_0">
<AgreementId>987456123</AgreementId>
</MessageHeader>
<StoreOrderRequest xmlns="">
<AccountGroupId>123456789</AccountGroupId>
</StoreOrderRequest>
</m:StoreOrderRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
But I want below result Can anyone please guide me what mistakes I have done in code.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns="http://test.com/schema/OrderingMobility/v8_0"
xmlns:SOAPENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<m:StoreOrderRequest xmlns:m="http://test.com/wsdl/OrderingMobility/v8_0">
<MessageHeader xmlns="http://test.com/schema/MsgHeader/v1_0">
<AgreementId>987654321</AgreementId>
</MessageHeader>
<StoreOrderRequest>
<AccountGroupId>123456789</AccountGroupId>
</StoreOrderRequest>
</m:StoreOrderRequest>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>