XmlSerializer problem with namespace

Noah Aas 360 Reputation points
2024-07-03T18:48:11.1166667+00:00

Hello!

I need to deserialize and serialize this XML.
Unfortunately without success.
Why, who can solve it? Where is the cause?
Target

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

Actual state

<COM_1>
  <body>
    <Z_COATING_ORDER>
      <IM_BACK>Rü-TEST</IM_BACK>
    </Z_COATING_ORDER>
  </body>
</COM_1>

: missing xmlns:doc missing and so one. // [XmlRoot(ElementName = "COM_1")] // I use this.

[XmlRoot("doc", Namespace = "urn:biztalk-org:biztalk:biztalk_1")]
public class COM1
<q1:doc xmlns:q1="urn:biztalk-org:biztalk:biztalk_1">
  <q1:body>
    <q1:Z_COATING_ORDER>
      <q1:IM_BACK>Rü-TEST</q1:IM_BACK>
    </q1:Z_COATING_ORDER>
  </q1:body>
</q1:doc>

Where does the q1 come from? What is the best way to go for it?

My unsuccessful attempts.

  private void btnReSAP_Click(object sender, EventArgs e)
        {
//<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>
            COM1 tstRe = new COM1();
            tstRe.Xmlns = "urn:biztalk-org:biztalk:biztalk_1";
            tstRe.Body = new Body();
            tstRe.Body.ZCOATINGORDER = new ZCOATINGORDER();
            tstRe.Body.ZCOATINGORDER.Xmlns = "urn:sap-com:document:sap:rfc:functions";
            tstRe.Body.ZCOATINGORDER.IMBACK = "Rü-TEST";





 using System.Xml.Serialization;
 XmlSerializer serializer = new XmlSerializer(typeof(COM1));
 using (StringReader reader = new StringReader(xml))
 {
    var test = (COM1)serializer.Deserialize(reader);
 }

[XmlRoot(ElementName="Z_COATING_ORDER")]
public class ZCOATINGORDER { 

	[XmlElement(ElementName="IM_BACK")] 
	public string IMBACK { get; set; } 

	[XmlAttribute(AttributeName="doc")] 
	public string Doc { get; set; } 

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

	[XmlText] 
	public string Text { get; set; } 
}

[XmlRoot(ElementName="body")]
public class Body { 

	[XmlElement(ElementName="Z_COATING_ORDER")] 
	public ZCOATINGORDER ZCOATINGORDER { get; set; } 
}

[XmlRoot(ElementName="COM_1")]
public class COM1 { 

	[XmlElement(ElementName="body")] 
	public Body Body { get; set; } 

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

	[XmlText] 
	public string Text { get; set; } 
}

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,574 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 60,376 Reputation points
    2024-07-03T22:05:49.1833333+00:00

    in xml the namespace is the url. the prefix is an alias (which is random, and has no value). the xml will define the alias:

    in your first sample:

    <COM_1 xmlns="urn:biztalk-org:biztalk:biztalk_1">
    	<body>
    		<doc:Z_COATING_ORDER xmlns:doc="urn:sap-com:doc:sap:rfc:functions" xmlns="">
    			<IM_BACK>#_DATA_#</IM_BACK>
    		</doc:Z_COATING_ORDER >
    	</body>
    </COM_1>
    
    • xmlns="urn:biztalk-org:biztalk:biztalk_1" - defines namespace with no prefix. so it the namespace for the node
    • xmlns:doc="urn:sap-com:doc:sap:rfc:functions" - defines doc as prefix to the namespace. so any node that is prefixed as "doc:" is using this namespace

    with prefixes specified and not implied its the same as (biz and doc are arbitrary, any value can be used):

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

    with your code:

    [XmlRoot("doc", Namespace = "urn:biztalk-org:biztalk:biztalk_1")]
    public class COM1
    

    you define "doc" as the name of the node, and "urn:biztalk-org:biztalk:biztalk_1" as its namespace name. the serializer makes up the "q1" as the alias.

    again the following is semantically the same as specifying the alias:

    <doc xmlns:"urn:biztalk-org:biztalk:biztalk_1">
      <body>
        <Z_COATING_ORDER>
          <IM_BACK>Rü-TEST</q1:IM_BACK>
        <Z_COATING_ORDER>
      </body>
    </doc>
    

    note: to handle multiple namespaces, the xml serializer aways uses the prefix mode and doesn't count on inhertance. also when paring, you only specify the valid namespace, not the prefix alias


0 additional answers

Sort by: Most helpful