Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to remove all namespace prefix from an xml node dynamically in c#. I am able to remove the same using the replace function in a static manner.
Namespace prefix may vary. I want to remove the prefix from Node name T24EnuiryResponse in the xml updated below:
Code tried is as below:
<pre>NodeValueFromStreamWithoutNS=GetNodeValueFromStream(streamR, NodeName).Replace("ns1:", "").Replace("ns2:", "").Replace("ns3:", "");


What I have tried:

Code is as below:
NodeValueFromStreamWithoutNS=GetNodeValueFromStream(streamR, NodeName).Replace("ns1:", "").Replace("ns2:", "").Replace("ns3:", "");

Response Xml:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
	xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
	<soapenv:Body>
		<ser-root:T24EnquiryResponse
			xmlns:ser-root="https://soa.nbf.ae/Enquiry/"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
			<Status>SUCCESS</Status>
			<EnquiryOutput>
				<NBFDDSLOANLOOKUP>
					<gNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID>MG1827000105</ID>
							<CUSTOMERID>555312</CUSTOMERID>
							<PRINLIQACCT>012001312705</PRINLIQACCT>
							<DRAWDOWNACCOUNT>012001304621</DRAWDOWNACCOUNT>
							<CURRENCY>AED</CURRENCY>
							<AMOUNT>300000.00</AMOUNT>
							<LOANSTATUS>LIQ</LOANSTATUS>
							<OVERDUESTATUS>NAB</OVERDUESTATUS>
							<DUEMONTH>202009</DUEMONTH>
							<MONTHDUEAMT>20868.73</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202008</DUEMONTH>
							<MONTHDUEAMT>19261.65</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202007</DUEMONTH>
							<MONTHDUEAMT>19501.96</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202006</DUEMONTH>
							<MONTHDUEAMT>19734.51</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202005</DUEMONTH>
							<MONTHDUEAMT>19974.83</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202004</DUEMONTH>
							<MONTHDUEAMT>20207.38</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202003</DUEMONTH>
							<MONTHDUEAMT>20447.7</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202002</DUEMONTH>
							<MONTHDUEAMT>20680.25</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>202001</DUEMONTH>
							<MONTHDUEAMT>20912.82</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
						<mNBFDDSLOANLOOKUPDetailType>
							<ID/>
							<CUSTOMERID/>
							<PRINLIQACCT/>
							<DRAWDOWNACCOUNT/>
							<CURRENCY/>
							<AMOUNT/>
							<LOANSTATUS/>
							<OVERDUESTATUS/>
							<DUEMONTH>201912</DUEMONTH>
							<MONTHDUEAMT>75012.51</MONTHDUEAMT>
						</mNBFDDSLOANLOOKUPDetailType>
					</gNBFDDSLOANLOOKUPDetailType>
				</NBFDDSLOANLOOKUP>
			</EnquiryOutput>
		</ser-root:T24EnquiryResponse>
	</soapenv:Body>
</soapenv:Envelope>
Posted
Updated 13-Apr-22 2:50am
v2
Comments
Rajeev Jayaram 13-Apr-22 4:55am    
What do you mean by "Since Namespace prefixes came different in another environment"?
Member 7513082 13-Apr-22 6:23am    
namespace prefix may com as ns3: or as ser-root etc for the node T24EnquiryResponse. I need to remove the namespace prefix whichever it may be for this node name alone.

1 solution

It is a SOAP response packet, so I am not sure why you would want to do that.

This may point you in the right direction: [Solved] How to read soap response in c#? - CodeProject[^]
 
Share this answer
 
Comments
Member 7513082 18-Apr-22 7:36am    
I am able to remove the namespace with below code but the data with in the node are lost, I need to keep the node data as such even after removal of the namespace
var soapBody = doc.GetElementsByTagName("soapenv:Body")[0];
string innerObject = soapBody.InnerXml;
if (innerObject.Contains("T24EnquiryResponse"))
{
XElement xElement = new XElement(XElement.Parse(innerObject).Name.LocalName);
string test = xElement.Name.ToString();
string test1= xElement.Name.LocalName;
xElement.Name = xElement.Name.LocalName.ToString();

xElement = new XElement(xElement.Name.LocalName, XElement.Parse(innerObject).Elements().Select(el => RemoveAllNamespaces(el)));
//xmlDocumentWithoutNs = xElement;
txtRequest.Text = xElement.ToString();
}

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