How to translate this JS to C#

I have the following JS code..
How do I translate this to C#?

I think Dictionary is the same as MAP in C#, what about parmas.get  and keyset??

(getAuthTemplateTag returns a constant- not an issue)


public final String createAuthTemplateXML(Map<String, String> params) {
            StringBuilder sb = new StringBuilder();

            for (String key : params.keySet()) {

            return sb.toString();
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robert SchuttSoftware EngineerCommented:
That looks like Java, not JavaScript.

Here is the equivalent C# code:
        private void button1_Click(object sender, EventArgs e) { // just for testing
            Dictionary<String, String> p = new Dictionary<string,string>();
            p.Add("x", "y");
            textBox1.Text = createAuthTemplateXML(p); // result: <authRequest><xyz><x>y</x></xyz></authRequest>

        public String createAuthTemplateXML(Dictionary<String, String> _params) {
            StringBuilder sb = new StringBuilder();

            foreach (String key in _params.Keys) {

            return sb.ToString();

        public String getAuthTemplateTag() {
            return "xyz";

Open in new window

Note that params is a reserved word in C# so I prefixed it with an underscore.

Note also that this will return invalid xml when the dictionary contains values with characters that are not allowed at that point for example html tags but also possible encoding issues. In general you should use XML objects to build the document (only thing missing are the newlines), maybe you can even use that directly instead of using a string in the rest of your code:
        public String createAuthTemplateXML(Dictionary<String, String> _params) {
            XmlDocument xmlDoc = new XmlDocument();
            XmlElement nodeRoot = xmlDoc.CreateElement("authRequest");
            XmlElement nodeAuthTemplate = xmlDoc.CreateElement(getAuthTemplateTag());
            foreach (String key in _params.Keys) {
                XmlElement nodeKey = xmlDoc.CreateElement(key);
                nodeKey.InnerText = _params[key];
            return xmlDoc.OuterXml;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JElsterAuthor Commented:
Wow... you're amazing.. thx again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.