C#如何创建一个XML文件 格式是 <A> <B C="D">E<⼀B> <⼀A>

2025-02-23 15:03:29
推荐回答(1个)
回答1:

        System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
        
        System.Xml.XmlDeclaration dec = xml.CreateXmlDeclaration("1.0", "UTF-8", null);
        xml.AppendChild(dec);
        System.Xml.XmlElement ele = xml.CreateElement("A");
        xml.AppendChild(ele);
        System.Xml.XmlElement ele2 = xml.CreateElement("B");
        
        System.Xml.XmlAttribute xa = xml.CreateAttribute("C");
        xa.Value = "D";
        ele2.Attributes.Append(xa);
        ele2.InnerXml= "E";
        ele.AppendChild(ele2);

        xml.Save(Server.MapPath("~/1.xml"));