0

I was writing xml with c#, and I come up with a very long list of the same function- SetAttribute, wonder if anyone knows a smart way to write this code. I think about making an array but donot know how, anyone know how to write an array for this code?

        XmlDocument doc = new XmlDocument();
        XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-16", null);
        doc.AppendChild(decl);
        XmlElement ChatMapper = doc.CreateElement("ChatMapperProject");  
        doc.AppendChild(ChatMapper);
        XmlNode xmldocSelect = doc.SelectSingleNode("ChatMapperProject");
        //Crteate Attribute
        ChatMapper.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        ChatMapper.SetAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
        ChatMapper.SetAttribute("Title", "");
        ChatMapper.SetAttribute("Version", "1.5.1.0");
        ChatMapper.SetAttribute("Author", "");
        ChatMapper.SetAttribute("EmphasisColor1Label", "");
        ChatMapper.SetAttribute("EmphasisColor1", "#000000");
        ChatMapper.SetAttribute("EmphasisStyle1", "---");
        ChatMapper.SetAttribute("EmphasisColor2Label", "");
        ChatMapper.SetAttribute("EmphasisColor2", "#000000");
        ChatMapper.SetAttribute("EmphasisStyle2", "---");
        ChatMapper.SetAttribute("EmphasisColor3Label", "");
        ChatMapper.SetAttribute("EmphasisColor3", "#000000");
        ChatMapper.SetAttribute("EmphasisStyle3", "---");
        ChatMapper.SetAttribute("EmphasisColor4Label", "");
        ChatMapper.SetAttribute("EmphasisColor4", "#000000");
        ChatMapper.SetAttribute("EmphasisStyle4", "---");
Eleanor
  • 569
  • 1
  • 8
  • 17
  • 1
    why don't you just [Serialize an Object to XML](http://stackoverflow.com/questions/4123590/serialize-an-object-to-xml). another [MSDN tutorial](https://support.microsoft.com/en-us/kb/815813) doing the same thing – Liam May 12 '15 at 12:02
  • because I load one xml and adjust its value and then transform to another format xml, and almost done. – Eleanor May 12 '15 at 12:09

2 Answers2

0

You can create a dictionary object for attribute name as key and its value as value.

Run through the dictionary in a loop and add all the attributes.

But like Liam mentioned, its better if you can serialize the object itself, that would be the better solution.

Ashutosh Vyas
  • 419
  • 4
  • 17
  • This answer is correct but needs more details, see [how to answer](http://stackoverflow.com/help/how-to-answer) – Liam May 12 '15 at 12:14
0

Here is the sample code if you need.

XmlDocument doc = new XmlDocument();
            XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-16", null);
            doc.AppendChild(decl);
            XmlElement ChatMapper = doc.CreateElement("ChatMapperProject");
            doc.AppendChild(ChatMapper);
            XmlNode xmldocSelect = doc.SelectSingleNode("ChatMapperProject");
            //Crteate Attribute

            Dictionary<String, String> attributes = new Dictionary<string, string>();


            attributes.Add("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            attributes.Add("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
            attributes.Add("Title", "");
            attributes.Add("Version", "1.5.1.0");
            attributes.Add("Author", "");
            attributes.Add("EmphasisColor1Label", "");
            attributes.Add("EmphasisColor1", "#000000");
            attributes.Add("EmphasisStyle1", "---");
            attributes.Add("EmphasisColor2Label", "");
            attributes.Add("EmphasisColor2", "#000000");
            attributes.Add("EmphasisStyle2", "---");
            attributes.Add("EmphasisColor3Label", "");
            attributes.Add("EmphasisColor3", "#000000");
            attributes.Add("EmphasisStyle3", "---");
            attributes.Add("EmphasisColor4Label", "");
            attributes.Add("EmphasisColor4", "#000000");
            attributes.Add("EmphasisStyle4", "---");


            foreach (KeyValuePair<String, String> attribute in attributes)
            {
                ChatMapper.SetAttribute(attribute.Key, attribute.Value);
            }
Ashutosh Vyas
  • 419
  • 4
  • 17
  • Thanks a lot, but Could be an array for the attributes.Add as well? – Eleanor May 12 '15 at 12:28
  • An array if you use would have to be possibly a 2D array to hold both attribute name and its value. Any specific reason why you would prefer Arrays ?? – Ashutosh Vyas May 12 '15 at 12:29
  • I want to make it shorter, smarter. writing the same time over and over again is not that good...I think – Eleanor May 12 '15 at 12:32
  • Since you have the attributes defined, you will have to for once write them off. Be it in an array or in a Dictionary or directly to SetAttributes. Even if you use array, you will have to fill it atleast once and that code will have to be a repetetive one. – Ashutosh Vyas May 12 '15 at 12:33
  • may be a function array is better? – Eleanor May 12 '15 at 12:49