Click here to Skip to main content
15,936,349 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to add node prefix to an existing request xml string in c#

Current Xml:
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>
			<CR_NAME>FOOD SUPPLIES LLC</CR_NAME>
	 </Dr-Dd-Mandate-Master-Full>
	</FCUBS_BODY>
</CREATEPMDEBTORMANDATE_FSFS_REQ>


Desired Xml
XML
<pmd:CREATEPMDEBTORMANDATE_FSFS_REQ>
	<pmd:FCUBS_HEADER>
		<pmd:SOURCE>DDS</pmb:SOURCE>
		</pmd:FCUBS_HEADER>
	<pmd:FCUBS_BODY>
		<pmd:Dr-Dd-Mandate-Master-Full>
			<pmd:CR_AC_CCY>AED</pmd:CR_AC_CCY>
		</pmd:Dr-Dd-Mandate-Master-Full>
	</pmd:FCUBS_BODY>
</pmd:CREATEPMDEBTORMANDATE_FSFS_REQ>


What I have tried:

Current Xml:
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>
			<CR_NAME>FOOD SUPPLIES LLC</CR_NAME>
	 </Dr-Dd-Mandate-Master-Full>
	</FCUBS_BODY>
</CREATEPMDEBTORMANDATE_FSFS_REQ>


Desired Xml
XML
<pmd:CREATEPMDEBTORMANDATE_FSFS_REQ>
	<pmd:FCUBS_HEADER>
		<pmd:SOURCE>DDS</pmb:SOURCE>
		</pmd:FCUBS_HEADER>
	<pmd:FCUBS_BODY>
		<pmd:Dr-Dd-Mandate-Master-Full>
			<pmd:CR_AC_CCY>AED</pmd:CR_AC_CCY>
		</pmd:Dr-Dd-Mandate-Master-Full>
	</pmd:FCUBS_BODY>
</pmd:CREATEPMDEBTORMANDATE_FSFS_REQ>
Posted
Updated 2-Aug-23 5:50am
v2

1 solution

You can use an XML library like 'XDocument' Class, using 'System.Xml.Linq' - MS Learn | XDocument Class[^]

You need to load your existing XML string into an 'XDocument'.
You can then modify the element names by adding the prefix 'pmd'.
Then convert your modified 'XDocument' back to a string -

C#
using System;
using System.Xml.Linq; //See my link for a proper tutorial and sample code...

class Program
{
    static void Main()
    {
        //Your existing XML string...
        string existingXml = @"<CREATEPMDEBTORMANDATE_FSFS_REQ>
                                <FCUBS_HEADER>
                                    <SOURCE>DDS</SOURCE>
                                </FCUBS_HEADER>
                                <FCUBS_BODY>
                                    <Dr-Dd-Mandate-Master-Full>
                                        <CR_AC_CCY>AED</CR_AC_CCY>
                                        <CR_NAME>FOOD SUPPLIES LLC</CR_NAME>
                                    </Dr-Dd-Mandate-Master-Full>
                                </FCUBS_BODY>
                            </CREATEPMDEBTORMANDATE_FSFS_REQ>";

        //Load the existing XML string into an XDocument...
        XDocument xdoc = XDocument.Parse(existingXml);

        //Add the 'pmd:' prefix to the element names...
        AddPrefix(xdoc.Root, "pmd");

        //Convert your modified XDocument back to a string...
        string modifiedXml = xdoc.ToString();

        //Print the modified XML or use where you need it...
        Console.WriteLine(modifiedXml);
    }

    //Recursive method to add the prefix to all elements and their descendants...
    static void AddPrefix(XElement element, string prefix)
    {
        element.Name = XName.Get(prefix + ":" + element.Name.LocalName, element.Name.NamespaceName);
        foreach (var child in element.Elements())
        {
            AddPrefix(child, prefix);
        }
    }
}
 
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