Hi @Noah Aas , Welcome to Microsoft Q&A,
It is possible to make some tweaks to the C# class and use 'XmlSerializer' for serialization.
using System;
using System.Xml.Serialization;
using System.IO;
public class ZCOATINGORDER
{
[XmlElement(ElementName = "IM_BACK")]
public string IMBACK { get; set; }
}
[XmlRoot(ElementName = "body")]
public class Body
{
[XmlElement(ElementName = "Z_COATING_ORDER", Namespace = "urn:sap-com:document:sap:rfc:functions")]
public ZCOATINGORDER ZCOATINGORDER { get; set; }
}
[XmlRoot("COM_1", Namespace = "urn:biztalk-org:biztalk:biztalk_1")]
public class COM1
{
[XmlElement(ElementName = "body")]
public Body Body { get; set; }
private XmlSerializerNamespaces _Namespaces;
public XmlSerializerNamespaces NameSpaces
{
get
{
if (_Namespaces == null)
{
_Namespaces = new XmlSerializerNamespaces();
_Namespaces.Add("q1", "urn:biztalk-org:biztalk:biztalk_1");
_Namespaces.Add("doc", "urn:sap-com:document:sap:rfc:functions");
}
return _Namespaces;
}
}
}
Serialize to XML
public class Program
{
public static void Main(string[] args)
{
COM1 com1 = new COM1
{
Body = new Body
{
ZCOATINGORDER = new ZCOATINGORDER
{
IMBACK = "Rü-TEST"
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(COM1));
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
OmitXmlDeclaration = true
};
using (StringWriter textWriter = new StringWriter())
{
using (XmlWriter xmlWriter = XmlWriter.Create(textWriter, settings))
{
serializer.Serialize(xmlWriter, com1, com1.NameSpaces);
}
string xmlOutput = textWriter.ToString();
Console.WriteLine(xmlOutput);
}
}
}
Output:
<q1:COM_1 xmlns:q1="urn:biztalk-org:biztalk:biztalk_1">
<q1:body>
<doc:Z_COATING_ORDER xmlns:doc="urn:sap-com:document:sap:rfc:functions">
<IM_BACK>Rü-TEST</IM_BACK>
</doc:Z_COATING_ORDER>
</q1:body>
</q1:COM_1>
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.