C#之创建Xml

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace __杂集
{
    class Program
    {
        string path = "";
        /// <summary>
        /// 打印日志
        /// </summary>
        /// <param name="str"></param>
        static void  Log(string str){
            Console.WriteLine(str);
        }

        static void Main(string[] args)
        {
            Program pro = new Program();
            //获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
            pro.path = System.AppDomain.CurrentDomain.BaseDirectory;
            Log(pro.path);

            pro.CreateXmlDemo();

            Console.ReadKey();
        }


        void CreateXmlDemo()
        {
            List<XmlData> xmlList = new List<XmlData>();
            for (int i = 0; i < 12; i++ )
            {
                XmlData xd = new XmlData(i, "name_" + i);
                xmlList.Add(xd);
            }

            Log("Type is:" + xmlList.GetType());
            CrateXml.CreateXmlByParam(path, xmlList);
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
using System.Xml;

//https://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer(v=vs.110).aspx
namespace __杂集
{

    class CrateXml
    {

        static string version = "1.0";
        static bool isMutiple = false;
        public static void CreateXmlByParam(string path, Object obj)
        {
            //文件流
            FileStream fs = null;
            try
            {
                //创建文件流
                fs = new FileStream(path + "test.xml", FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                //xmlm命名空间
                XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
                xmlNameSpace.Add("v", version);
                xmlNameSpace.Add("multiple", isMutiple.ToString());
                //XmlSerializer:serializes and deserializes objects into and from XML documents. The XmlSerializer enables you to control how objects are encoded into XML.
                XmlSerializer serizer = new XmlSerializer(obj.GetType());
                //https://msdn.microsoft.com/en-us/library/system.xml.xmlwritersettings(v=vs.110).aspx
                //xml写入设置
                XmlWriterSettings xmlWriteSetting = new XmlWriterSettings();
                //编码方式为UITF8
                xmlWriteSetting.Encoding = Encoding.UTF8;
                //排版
                xmlWriteSetting.Indent = true;
                //把内容序列化到xml中去
                serizer.Serialize(XmlWriter.Create(fs, xmlWriteSetting), obj, xmlNameSpace);
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (fs != null)
                    fs.Close();

            }

        }
    }

    public class XmlData
    {
        public int id;
        public string name;

        public XmlData(int id, string name)
        {
            this.id = id;
            this.name = name;
        }

        public XmlData()
        {
        }
    }

}



发布了70 篇原创文章 · 获赞 68 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/Rose_Girls/article/details/52043226