XmlSerializer problem with prefix and namespace

Noah Aas 360 Reputation points
2024-07-04T17:19:54.7+00:00

Hello, Is there a way to generate this output via serialization?

<COM_1 xmlns="urn:biztalk-org:biztalk:biztalk_1">
	<body>
		<doc:Z_COATING_ORDER xmlns:doc="urn:sap-com:document:sap:rfc:functions" xmlns="">
			<IM_BACK>#_DATA_#</IM_BACK>
		</doc:Z_COATING_ORDER >
	</body>
</COM_1>

I need to send this request via a REST interface. The customer gave me this specification.

biz and doc are arbitrary, any value can be used Is this specified in the XML?

Prefix can be q1, doc and so on. The other person does not evaluate this. There are no exchange conflicts here. What do you use it for then? What is it good for?

Current state.

<q1:COM_1 xmlns:q1="urn:biztalk-org:biztalk:biztalk_1">
  <q1:body>
    <Z_COATING_ORDER xmlns="urn:sap-com:document:sap:rfc:functions">
      <IM_BACK>Rü-TEST</IM_BACK>
    </Z_COATING_ORDER>
  </q1:body>
</q1:COM_1>
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; }

	[XmlAttribute(AttributeName = "xmlns")]
	public string Xmlns { get; set; }        

	private XmlSerializerNamespaces _Namespaces;
	public XmlSerializerNamespaces NameSpaces
	{
		get
		{
			if (_Namespaces == null)
			{
				_Namespaces = new XmlSerializerNamespaces();
				_Namespaces.Add("ns0", "urn:biztalk-org:biztalk:biztalk_1");
				_Namespaces.Add("ns1", "urn:sap-com:document:sap:rfc:functions");
			}
			return _Namespaces;
		}
	}
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,579 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ganeshkumar R 510 Reputation points
    2024-07-04T17:29:40.0933333+00:00

    Adjusted C# Code

    1. Define Classes with Proper Namespace Prefixes:
      • Use XmlType and XmlElement attributes to specify namespaces.
      • Use XmlSerializerNamespaces to add namespace prefixes.
    
    using System;
    
    using System.IO;
    
    using System.Xml.Serialization;
    
    public class ZCOATINGORDER
    
    {
    
        [XmlElement(ElementName = "IM_BACK")]
    
        public string IMBACK { get; set; }
    
    }
    
    public class Body
    
    {
    
        [XmlElement(ElementName = "Z_COATING_ORDER", Namespace = "urn:sap-com:document:sap:rfc:functions")]
    
        public ZCOATINGORDER ZCOATINGORDER { get; set; }
    
    }
    
    [XmlRoot(ElementName = "COM_1", Namespace = "urn:biztalk-org:biztalk:biztalk_1")]
    
    public class COM1
    
    {
    
        [XmlElement(ElementName = "body")]
    
        public Body Body { get; set; }
    
        [XmlNamespaceDeclarations]
    
        public XmlSerializerNamespaces Namespaces { get; set; }
    
        public COM1()
    
        {
    
            Namespaces = new XmlSerializerNamespaces();
    
            Namespaces.Add("q1", "urn:biztalk-org:biztalk:biztalk_1");
    
            Namespaces.Add("doc", "urn:sap-com:document:sap:rfc:functions");
    
        }
    
    }
    
    
    1. Serialize to XML:
      • Serialize the COM1 object to XML using XmlSerializer.
    
    public class Program
    
    {
    
        public static void Main()
    
        {
    
            // Create the object with the data to serialize
    
            var com1 = new COM1
    
            {
    
                Body = new Body
    
                {
    
                    ZCOATINGORDER = new ZCOATINGORDER
    
                    {
    
                        IMBACK = "#_DATA_#"
    
                    }
    
                }
    
            };
    
            // Serialize the object to XML
    
            var serializer = new XmlSerializer(typeof(COM1));
    
            using (var stringWriter = new StringWriter())
    
            {
    
                serializer.Serialize(stringWriter, com1);
    
                var xmlString = stringWriter.ToString();
    
                Console.WriteLine(xmlString);
    
            }
    
        }
    
    }
    
    

    Output:

    The above code will produce the following XML:

    
    <?xml version="1.0" encoding="utf-16"?>
    
    <q1:COM_1 xmlns:q1="urn:biztalk-org:biztalk:biztalk_1" xmlns:doc="urn:sap-com:document:sap:rfc:functions">
    
      <q1:body>
    
        <doc:Z_COATING_ORDER>
    
          <IM_BACK>#_DATA_#</IM_BACK>
    
        </doc:Z_COATING_ORDER>
    
      </q1:body>
    
    </q1:COM_1>
    
    

    This XML matches the desired output format, including the namespace prefixes for COM_1 and Z_COATING_ORDER.

    0 comments No comments

  2. Jiale Xue - MSFT 42,151 Reputation points Microsoft Vendor
    2024-07-05T07:27:57.68+00:00

    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.