We help IT Professionals succeed at work.

How to translate this JS to C#

JElster
JElster asked
on
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();
      }
Comment
Watch Question

Software Engineer
Commented:
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

Author

Commented:
Wow... you're amazing.. thx again!