XML,xml檔案怎麼開啟
XML的文檔描述
<?xml version="1.0" encoding="utf-8">
每一個XML文檔必須有,並且只能有一個跟節點(xml大小寫敏感)
標籤一般成對存在,例如:<a></a>等,但是如果標籤沒有子資料可以寫成封閉的 例如<a />
XML的儲存結構有以下兩種
<標籤名>值</標籤名>
<標籤名 屬性="值" 屬性2="值"/>
例如:
<person name="張三" age="25" Gender="1"/>
========================================
xml使用 <!--這是注釋內容--> 進行注釋
<?xml version="1.0" encoding="utf-8"?><studetn><stuinfo id="001"><name>張三</name><age>25</age><Gender>1</Gender></stuinfo><stuinfo id="002"><name>李四</name><age>25</age><Gender>1</Gender></stuinfo><stuinfo id="003"><name>王五</name><age>25</age><Gender>1</Gender></stuinfo></studetn>
xml 的另外一種注釋:<![CDATA[..........]]>
<news><title>標題</title><content><a href="http://www.baidu.com">今天的頭條新聞</a></content> <!--問題來了,我這個content標籤裡面只是想放一個超連結作為內容,可是我這個超連結是一個<a></a>這樣的成對出現的,xml會把它識別成一個節點,那我不像讓這個a標籤變成一個節點,僅僅是把他當成內容,怎麼辦呢?--></news>
請看下面的這種注釋方式:<![CDATA[..........]]>
<?xml version="1.0" encoding="gb2312"?><news><title>標題</title><content><![CDATA[<a href="http://www.baidu.com">今天的頭條新聞</a>]]></content> </news>
用C#語句建立一個XML文檔
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml;namespace xml{ class Program { /// <summary> /// 建立一個XML文檔 /// </summary> /// <param name="args"></param> static void Main(string[] args) { //命名空間:System.Xml; //類庫: XmlDocument 文檔 // XmlElement 元素 // XmlAttribute 屬性 // XmlText 內容 XmlDocument xdoc = new XmlDocument(); //表示 XML 文檔。 //所有的元素使用文檔節點建立 XmlDeclaration xdec = xdoc.CreateXmlDeclaration("1.0", "gb2312", null); xdoc.AppendChild(xdec); //將指定的節點添加到該節點的子節點列表的末尾。 XmlElement xele = xdoc.CreateElement("root"); xdoc.AppendChild(xele); //將指定的節點添加到該節點的子節點列表的末尾。 XmlAttribute xAttr = xdoc.CreateAttribute("id"); xAttr.Value = "101"; //擷取或設定節點的值。 XmlText txt = xdoc.CreateTextNode("我是一個文本節點"); xele.AppendChild(txt); //將指定的節點添加到該節點的子節點列表的末尾。 xele.Attributes.Append(xAttr); //將指定的特性插入集合,並將其作為集合中的最後一個節點。 xdoc.Save("D:/XML.XML"); } }}
建立好的XML文檔內容:
查到節點: 例如在我的E盤下有一個xml.xml的檔案,我要根據某個值擷取它的父節點
<?xml version="1.0" encoding="utf-8"?><studetn><stuinfo id="001"><name>張三</name><age>25</age><Gender>1</Gender></stuinfo><stuinfo id="002"><name>李四</name><age>25</age><Gender>1</Gender></stuinfo><stuinfo id="003"><name>王五</name><age>25</age><Gender>1</Gender></stuinfo></studetn>
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Xml.Linq;namespace ReadXml{ class Program { /// <summary> /// 根據一個內容的值,來尋找父節點(擷取父節點的所有內容) /// </summary> /// <param name="args"></param> static void Main(string[] args) { XDocument xdoc= XDocument.Load("e:/xml.xml");//從某個檔案中去載入xml文檔 //將查到到的內容放到XElement中,所以我們建立一個XElement的泛型集合,因為我們尋找到的節點可能有多個 List<XElement> list = new List<XElement>(); SearchElements(xdoc.Root, list); } public static void SearchElements(XElement ele, List<XElement> list) { //首先遍曆ele的所有子節點 foreach (XElement item in ele.Elements()) //Elements()表示ele這個節點的所有子節點.即:子項目集合。 { //判斷這個元素的名字是否為name,如果name裡存的值是“張三” if (item.Name.LocalName == "name") { if (item.Value == "張三") { list.Add(item.Parent);//將"張三"這個節點的負節點添加到list中去 } } //如果item裡面還有子節點就遞迴(關於遞迴可以參考asp.net文章中的<a target=_blank href="http://blog.csdn.net/fanbin168/article/details/45623263" target="_blank">TreeView 樹菜單 節點</a><span class="gray"></span>) SearchElements(item, list); } } }}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。