C#生成plist文件

在IOS开发过程中,一些应用包括游戏为了生成不同的场景布局很多时候都会使用plist作为配置文件,先由设计人员设计好布局,再按照布局创建plist文件,最后供程序开发人员调用,这是一个段很简单的C#生成plist文件的代码和大家分享一下,主要涉及XmlTextWriter的使用。(PS:plist文件本身就是XML文件)

        private void saveFileDialog2_FileOk(object sender, CancelEventArgs e)
        {
            // Create the file and writer.
            FileStream fs = new FileStream(saveFileDialog2.FileName, FileMode.Create);
            XmlTextWriter w = new XmlTextWriter(fs, Encoding.UTF8);

            // Start the document.
            w.WriteStartDocument();
            // Add commets
            w.WriteComment("DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd");
            //PLIST START
            w.WriteStartElement("plist");
            //PLIST VERSION
            w.WriteAttributeString("version", "1.0");
            //以上头部都是固定的,下面部分可以按需添加
            w.WriteStartElement("dict");

            w.WriteElementString("key", "row");//key名字为row
            w.WriteElementString("string", row.ToString());//想写入的数据
            w.WriteElementString("key", "col");
            w.WriteElementString("string", col.ToString());
            w.WriteElementString("key", "picKindNum");
            w.WriteElementString("string", picNum.ToString());
            w.WriteElementString("key", "picStartNo");
            w.WriteElementString("string", numStartNo.Value.ToString());
            w.WriteElementString("key", "picWidth");
            w.WriteElementString("string", picWidth.ToString());
            w.WriteElementString("key", "picHavaNum");
            w.WriteElementString("string", txtPicNum.Text);

            w.WriteElementString("key", "picMap");
            w.WriteStartElement("array");//这里有一个数组

            int i, j;
            for (i = row - 1; i >= 0; i--)
            {
                for (j = 0; j < col; j++)
                {
                    w.WriteElementString("string", pieces[i, j].Flag.ToString());//往数组里存数据
                }
            }

            // End the document.
            w.WriteEndElement();
            w.WriteEndElement();
            w.WriteEndDocument();
            w.Flush();
            fs.Close();
        }

你可能感兴趣的:(xml,String,object,C#,File,程序开发)