Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# c借助数组编写xml_C#_Xml - Fatal编程技术网

C# c借助数组编写xml

C# c借助数组编写xml,c#,xml,C#,Xml,我用c编写xml,我列出了一长串相同的函数-SetAttribute,不知道是否有人知道编写这段代码的聪明方法。我想做一个数组,但不知道怎么做,有人知道如何为这段代码编写数组吗 XmlDocument doc = new XmlDocument(); XmlDeclaration decl = doc.CreateXmlDeclaration("1.0", "utf-16", null); doc.AppendChild(decl);

我用c编写xml,我列出了一长串相同的函数-SetAttribute,不知道是否有人知道编写这段代码的聪明方法。我想做一个数组,但不知道怎么做,有人知道如何为这段代码编写数组吗

        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", "---");

您可以创建一个字典对象,将属性名称作为键,将其值作为值

在循环中运行字典并添加所有属性


但正如Liam所提到的,如果可以序列化对象本身就更好了,这将是更好的解决方案。

如果需要,这里是示例代码

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);
            }

你为什么不干脆。另一个也在做同样的事情,因为我加载了一个xml并调整了它的值,然后转换成另一种格式的xml,差不多完成了。这个答案是正确的,但需要更多的细节,请参阅,非常感谢,但可以是属性的数组。也可以添加?如果使用数组,则可能必须是一个2D数组来保存属性名称及其值。你喜欢阵列的具体原因是什么?我想让它更短、更智能。一次又一次地写同样的东西不是很好……我想既然你已经定义了属性,你就必须一次性地把它们注销。无论是在数组中,还是在字典中,还是直接到SetAttributes。即使使用数组,也必须至少填充一次,并且代码必须是重复的。函数数组可能更好吗?