Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to remove namespace during xml serialization in c#.
Xml currently got
<CREATEPMDEBTORMANDATE_FSFS_REQ>
	<FCUBS_HEADER
		xmlns="http://pmts.ofss.com/ws/PMDebtorMandate">
		<SOURCE>DDS</SOURCE>
		
	</FCUBS_HEADER>
	<FCUBS_BODY
		xmlns="http://pmts.ofss.com/ws/PMDebtorMandate">
		<Dr-Dd-Mandate-Master-Full>
			<CR_AC_CCY>AED</CR_AC_CCY>
			
		</Dr-Dd-Mandate-Master-Full>
	</FCUBS_BODY>
</CREATEPMDEBTORMANDATE_FSFS_REQ>


Expected Xml
<CREATEPMDEBTORMANDATE_FSFS_REQ>
	<FCUBS_HEADER>
		<SOURCE>DDS</SOURCE>
		
	</FCUBS_HEADER>
	<FCUBS_BODY>
		<Dr-Dd-Mandate-Master-Full>
			<CR_AC_CCY>AED</CR_AC_CCY>
			
		</Dr-Dd-Mandate-Master-Full>
	</FCUBS_BODY>
</CREATEPMDEBTORMANDATE_FSFS_REQ>


What I have tried:

My current Code is as below:
XmlDocument xmlDoc = new XmlDocument();
           XPathNavigator nav = xmlDoc.CreateNavigator();

           XmlSerializer ser = null;
           using (XmlWriter writer = nav.AppendChild())
           {
               XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
               ns.Add(string.Empty,string.Empty);
               if (Type == "CreateMandate")
               {
                   ser = XmlSerializerCache.Create(objList.GetType(), new XmlRootAttribute("CREATEPMDEBTORMANDATE_FSFS_REQ"));
               }
               ser.Serialize(writer, objList, ns);

           }
           string retXML = xmlDoc.InnerXml;

           xmlDoc = null;
           nav = null;

           return retXML;
Posted
Updated 29-Jul-23 3:35am
Comments
Member 15627495 28-Jul-23 6:37am    
use a LINQ query to pick the xml section you need,
then apply serialization.

as Xml, and Xdocument are trees, you can query Xml Trees, and get the datas you want.
Once serialized, the Tree structure is not reachable by Xml and LINQ to Xml features.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900