public partial class Form1 : Form
{ public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string path = @"D:\java\res\drawable-mdpi"; DirectoryInfo root = new DirectoryInfo(path); FileInfo[] files = root.GetFiles(); List<String> temp = new List<string>(); for (int i = 0; i < files.Length; i++) { string filename = files[i].Name.ToString(); temp.Add(filename.Substring(0,filename.Length-7)); } List<String> xx=temp.Distinct().ToList(); foreach (string x in xx) { // getxml(x); sssss(x); } } private void getxml(string sname) { //创建XmlDocument对象 XmlDocument xmlDoc = new XmlDocument(); //XML的声明<?xml version="1.0" encoding="gb2312"?> XmlDeclaration xmlSM = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); //追加xmldecl位置 xmlDoc.AppendChild(xmlSM); //添加一个名为Gen的根节点 XmlElement xml = xmlDoc.CreateElement("", "animation-list", ""); xml.SetAttribute("xmlns:android", "http://schemas.android.com/apk/res/android"); xml.SetAttribute("android:oneshot", "false"); //追加Gen的根节点位置 xmlDoc.AppendChild(xml); for (int i = 1; i < 22; i++) { //添加一个名为<Zi>的节点 XmlElement zi = xmlDoc.CreateElement("item"); //为<Zi>节点的属性 zi.SetAttribute("android:drawable", "@drawable/"+sname+"_"+String.Format("{0:D2}",i)); zi.SetAttribute("android:duration", "@string/ani_time"); xml.AppendChild(zi); } xmlDoc.Save("D:/"+sname+".xml"); } private void button2_Click(object sender, EventArgs e) { } private void sssss(string sname) { using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"D:/" + sname + ".xml", true)) { file.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");// 直接追加文件末尾,换行 file.WriteLine("<animation-list xmlns:android=\"http://schemas.android.com/apk/res/android\" android:oneshot=\"false\">"); for (int i = 1; i < 22; i++) { file.WriteLine(" <item android:drawable=\"@drawable/" + sname + "_" + String.Format("{0:D2}", i) + "\" android:duration=\"@string/ani_time\"/>"); } file.WriteLine("</animation-list>"); } } }