C#-XML-數據傳輸


http://www.cnblogs.com/fengxuehuanlin/p/5631664.html

關於xml是屬於一個比較重要的東西,在平時開發的過程中,這塊內容最主要的是要掌握XML內容的讀取和寫入操作。

xml可作為小型數據庫用來存儲數據。

html主要用來顯示數據,XAML前台設計。

一.什么是XML?

  • XML 指可擴展標記語言(EXtensible Markup Language)--可擴展意味着標簽可以自定義,不像html固定標簽。
  • XML 是一種標記語言,很類似HTML
  • XML 的設計宗旨是傳輸數據,而非顯示數據
  • XML 標簽沒有被預定義,您需要自行定義標簽
  • XML 被設計為具有自我描述性
  • XML 是W3C 的推薦標准

二.XML語法:

1.一個XML包含以下幾部分內容:

文檔聲明,元素,屬性,注釋,CDATA(特殊字符),處理指令,node節點

  • 節點包含元素(文本中見到的所有東西都是節點,元素指的是標簽)。
  • 只能有一個根節點
2.最簡單的聲明格式     
<?xml version="1.0" ?>
用encoding屬性聲明文檔的編碼     
<?xml version="1.0" encoding="UTF-8" ?>
用standalone屬性說明文檔是否獨立     
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 

三、寫入XML內容:

  • dom-document object model(XmlDocument文檔對象),注意必須先有一個根節點。
//通過代碼來創建XML文檔
            //1、引用命名空間
            //2、創建XML文檔對象
            XmlDocument doc = new XmlDocument();
            //3.創建第一行描述信息,添加到doc文檔中
           XmlDeclaration dec= doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            //4.創建根節點
            XmlElement siemens = doc.CreateElement("Siemens");
            doc.AppendChild(siemens);
            

            //5.給根節點books創建子節點
            XmlElement opcua = doc.CreateElement("OPCUA");
            siemens.AppendChild(opcua);

            //6.給book1添加子節點
            XmlElement plc1 = doc.CreateElement("PLC1");
            plc1.InnerText = "_84303";
            opcua.AppendChild(plc1);

            XmlElement plc2 = doc.CreateElement("PLC2");
            plc2.InnerText = "_87303";
            opcua.AppendChild(plc2);

            XmlElement plc3 = doc.CreateElement("PLC3");
            plc3.InnerText = "_89303";
            opcua.AppendChild(plc3);

            XmlElement plc4 = doc.CreateElement("PLC4");
            plc4.InnerXml = "<a>我就發而非</a>";
            opcua.AppendChild(plc4);

            XmlElement plc4_1 = doc.CreateElement("年級");
            plc4_1.SetAttribute("名字","小明");
            plc4_1.InnerXml = "學前班";
            plc4.AppendChild(plc4_1);
            doc.Save("Siemens.xml");

 

 XmlDocument doc2 = new XmlDocument();
            XmlDeclaration des2 = doc2.CreateXmlDeclaration("1.0","utf-8",null);
            doc2.AppendChild(des2);

            XmlElement person = doc2.CreateElement("Person");
            doc2.AppendChild(person);

            for(int i=0;i<lstudent.Count;i++)
            {
                XmlElement student = doc2.CreateElement("Studemt");
                student.SetAttribute("ID",lstudent[i].id.ToString());
                XmlElement name = doc2.CreateElement("Name");
                name.InnerXml = lstudent[i].name;
                XmlElement age = doc2.CreateElement("Age");
                age.InnerXml = lstudent[i].age.ToString();
                XmlElement gender = doc2.CreateElement("Gender");
                gender.InnerXml = lstudent[i].gender.ToString();

                person.AppendChild(student);
                student.AppendChild(name);
                student.AppendChild(age);
                student.AppendChild(gender);
            }

            doc2.Save("student.xml");

四.XML文件的讀取:

xml文件內容:

<?xml version="1.0" encoding="utf-8"?>
<library id="30">
  <BOOK id="20">
    <name>高等數學</name>
    <name1>大學英語</name1>
  </BOOK>
</library>

讀取XML內容:

static void Main(string[] args)
 {
     //將XML文件加載進來
     XDocument document = XDocument.Load("D:\\123.xml");
     //獲取到XML的根元素進行操作
     XElement root= document.Root;
     XElement ele= root.Element("BOOK");
     //獲取name標簽的值
     XElement shuxing= ele.Element("name");
     Console.WriteLine(shuxing.Value);
     //獲取根元素下的所有子元素
     IEnumerable<XElement> enumerable = root.Elements();
     foreach (XElement item in enumerable)
     {
         foreach (XElement item1 in item.Elements())
         {
             Console.WriteLine(item1.Name);   //輸出 name  name1            
         }
         Console.WriteLine(item.Attribute("id").Value);  //輸出20
     }   
     Console.ReadKey();
 }

五、增刪改查

  #region 增刪改查
            XmlDocument doc3 = new XmlDocument();
            if(File.Exists("student.xml"))
            {
                //存在
                //加載文件
                doc3.Load("student.xml");
                //追加,先要拿到根節點
                XmlElement Person = doc3.DocumentElement;
                #region 增加
                ////追加子節點
                //XmlElement student = doc3.CreateElement("Student");
                //student.SetAttribute("ID","5");
                //Person.AppendChild(student);
                //XmlElement ignore = doc3.CreateElement(".....");
                //student.AppendChild(ignore);
                #endregion
                #region 改查
                //改查1--獲取子節點的方法
                //XmlNodeList ndoes = Person.ChildNodes;
                //foreach(XmlNode xnode in ndoes)
                //{
                //    //不包含屬性 
                //    Console.WriteLine(xnode.InnerText);
                //    //屬性信息
                //    Console.WriteLine(xnode.Attributes["ID"].Value);
                //    if(xnode.Attributes["ID"].Value=="3")
                //    {
                //        xnode.Attributes["ID"].Value = "改";
                //    }
                //}
                //改查2---xmlpath的方法
                XmlNode node = Person.SelectSingleNode("/Person/Student");
                Console.WriteLine(node.InnerText);

                //直接找某一個子節點
                XmlNode node1 = Person.SelectSingleNode("/Person/Student[@ID='2']/Gender");
                node1.InnerText = "不男不女";
                Console.WriteLine(node1.InnerText);               
                Console.ReadKey();
                #endregion 改查
                #region 刪除
                //根節點不能刪除
               // doc3.RemoveAll();
                
                //移除的節點必須是操作節點的子集
                XmlNode node2 = Person.SelectSingleNode("/Person/Student[@ID='2']/Gender");
                //Person.RemoveChild(node2);
                XmlNode nodep = Person.SelectSingleNode("/Person/Student[@ID='2']/");
                nodep.RemoveChild(node2);

                //移除屬性
                nodep.Attributes.RemoveNamedItem("ID");
                #endregion

            }
            else
            {
                //不存在
                XmlDeclaration des3 = doc3.CreateXmlDeclaration("1.0", "utf-8", null);
                doc3.AppendChild(des3);
                //添加根節點
                XmlElement person3 = doc3.CreateElement("Person");
                doc3.AppendChild(person3);

            }

            doc3.Save("student.xml");
            #endregion

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM