1. 程式人生 > >【C# MVC工具類】DataSet/DataTable 與Xml檔案的互相轉化

【C# MVC工具類】DataSet/DataTable 與Xml檔案的互相轉化

無論做介面還是出於某種目的緩解伺服器壓力的時候,我們喜歡用xml檔案去做,這樣簡化了很多操作。但是在C#中如何操作呢?

一:DataSet/Datatable讀取xml檔案。
這個很簡單了,C#直接提供了api,我們直接呼叫就可以了:

DataSet ds = new DataSet();
ds.ReadXml(filePath + fileName);

當然我們要的可能不是DataSet或是DataTable,這樣就需要我們自己去解析xml檔案,xml檔案無非就是Node和Attribute兩個屬性。

二:根據DataSet/DataTable生成xml檔案。
這裡重點講一下生成xml,當然你知道DataSet如何生成xml,自然也就知道List如何生成xml,都是一樣的道理。
1). 使用linq to xml生成xml檔案:


     using System.Xml.Linq;       
     private void button1_Click(object sender, EventArgs e)  
     {  
         XNamespace ns = "http://www.xxx.com/XxxSystem";  
         XDocument doc = new XDocument(  
             new XDeclaration("1.0","UTF-8",null),  
             new XElement(ns + "Persons"
, new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"), new XElement("Person", new XAttribute("id","1"), new XElement("Name","xx"), new XElement("Age"
,xx) ), new XElement("Person", new XAttribute("id", "2"), new XElement("Name", "xx"), new XElement("Age", xx) ) ) ); doc.Save("person.xml"); }

2.)使用linq to xml 查詢xml資料:

using(StreamReader sr = new StreamReader("person.xml",Encoding.UTF8))
{
    XDocument doc = XDocument.Load(sr);
    XNameSpace ns = "heep:/www.xxxxx.cn.com/xxxx";
    var query = doc.Descendants(ns+"Psersons")
        .Elements(ns+"Person")
        .Where(p => p.Attribute("id").Value == "1")
        .Select(p => new 
        {
            Name = p.Element(ns+"Name").Value,
            Age = p.Element(ns+"Age").Value
        });
        foreach(var p in query)
        {
            Show(string.Format("{0}:{1}",p.Name,p.Age))
        }
}

3).使用XmlDocument生成xml檔案:
這裡使用的list作例子,沒有使用DataSet。

XmlDocument doc = new XmlDocument();
            doc.LoadXml("<UserMenu></UserMenu>");

            var parentlist = list.Where(p => p.Parent_Menu_List_Id.ToString() == "").OrderBy(p => p.Serial).ToList();
            for (int i = 0; i < parentlist.Count(); i++)
            {
                XmlElement elment = doc.CreateElement("HderMenu");
                XmlAttribute hMenuName = doc.CreateAttribute("MenuName");
                hMenuName.Value = parentlist[i].Request_Name;
                elment.Attributes.Append(hMenuName);

                XmlAttribute hMenuUrl = doc.CreateAttribute("MenuUrl");
                hMenuUrl.Value = parentlist[i].Request_URL;
                elment.Attributes.Append(hMenuUrl);

                XmlAttribute hMenuIcon = doc.CreateAttribute("MenuIcon");
                hMenuIcon.Value = parentlist[i].Icon_Name;
                elment.Attributes.Append(hMenuIcon);

                var childlist = list.Where(p => p.Parent_Menu_List_Id == parentlist[i].Menu_List_Id).OrderBy(p => p.Serial).ToList();
                for (int j = 0; j < childlist.Count(); j++)
                {
                    XmlElement subelment = doc.CreateElement("SubMenu");
                    XmlAttribute subMenuName = doc.CreateAttribute("MenuName");
                    subMenuName.Value = childlist[j].Request_Name;
                    subelment.Attributes.Append(subMenuName);

                    XmlAttribute subMenuUrl = doc.CreateAttribute("MenuUrl");
                    subMenuUrl.Value = childlist[j].Request_URL;
                    subelment.Attributes.Append(subMenuUrl);

                    XmlAttribute subMenuIcon = doc.CreateAttribute("MenuIcon");
                    subMenuIcon.Value = childlist[j].Icon_Name;
                    subelment.Attributes.Append(subMenuIcon);

                    elment.AppendChild(subelment);
                }
                doc.DocumentElement.AppendChild(elment);
            }

            doc.Save(path);