How to translate this JS to C#

Hi..
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)

thx

public final String createAuthTemplateXML(Map<String, String> params) {
            StringBuilder sb = new StringBuilder();
            sb.append("<authRequest>").append("\n").append("<")
                        .append(getAuthTemplateTag()).append(">").append("\n");

            for (String key : params.keySet()) {
                  sb.append("<").append(key).append(">").append(params.get(key))
                              .append("</").append(key).append(">").append("\n");
            }
            sb.append("</").append(getAuthTemplateTag()).append(">").append("\n")
                        .append("</authRequest>").append("\n");

            return sb.toString();
      }
LVL 1
JElsterAsked:
Who is Participating?
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();
            sb.Append("<authRequest>").Append("\n").Append("<")
                        .Append(getAuthTemplateTag()).Append(">").Append("\n");

            foreach (String key in _params.Keys) {
                  sb.Append("<").Append(key).Append(">").Append(_params[key])
                              .Append("</").Append(key).Append(">").Append("\n");
            }
            sb.Append("</").Append(getAuthTemplateTag()).Append(">").Append("\n")
                        .Append("</authRequest>").Append("\n");

            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];
                nodeAuthTemplate.AppendChild(nodeKey);
            }
            nodeRoot.AppendChild(nodeAuthTemplate);
            xmlDoc.AppendChild(nodeRoot);
            return xmlDoc.OuterXml;
        }

Open in new window

0

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!
0
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
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.