XML,xml檔案怎麼開啟

來源:互聯網
上載者:User

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);            }        }    }}







著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.