欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

使用XmlDocument怎么對Xml文檔進(jìn)行讀寫操作

這篇文章將為大家詳細(xì)講解有關(guān)使用XmlDocument怎么對Xml文檔進(jìn)行讀寫操作,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

站在用戶的角度思考問題,與客戶深入溝通,找到鹽邊網(wǎng)站設(shè)計(jì)與鹽邊網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、域名注冊、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋鹽邊地區(qū)。

通過XmlDocument讀寫Xml文檔

有如下一段Xml:

<?xml version="1.0" encoding="utf-8" ?>
<students>
  <!--我是一段注釋文字-->
  <student name="張平">
    <courses>
      <course name="語文?">
        <teacherComment>
          <![CDATA[
        這里是語文老師的批注
        ]]>
        </teacherComment>      
    </course>

      <course name="數(shù)學(xué)">
        <teacherComment>
          <![CDATA[
        這里是數(shù)學(xué)老師的批注
        ]]>
        </teacherComment>
      </course>
    </courses>
  </student>
</students>

1.如何使用XmlDocument讀取Xml

我要用一段代碼遍歷所有Student,并打印Student的所有屬性和子節(jié)點(diǎn)的值

/*玉開博客 http://www.php.cn/ */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace XmlExample
{
    class Program
    {
        static void Main(string[] args)
        {
            string xmlFilePath = @"X:\about.net\example\XmlExample\1.xml";
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlFilePath);

            //使用xpath表達(dá)式選擇文檔中所有的student子節(jié)點(diǎn)
            XmlNodeList studentNodeList = doc.SelectNodes("/students/student");
            if (studentNodeList != null)
            {
                foreach (XmlNode studentNode in studentNodeList)
                {
                    //通過Attributes獲得屬性名字為name的屬性
                    string name = studentNode.Attributes["name"].Value;
                    Console.WriteLine("Student:" + name);

                    //通過SelectSingleNode方法獲得當(dāng)前節(jié)點(diǎn)下的courses子節(jié)點(diǎn)
                    XmlNode coursesNode = studentNode.SelectSingleNode("courses");

                    //通過ChildNodes屬性獲得courseNode的所有一級子節(jié)點(diǎn)
                    XmlNodeList courseNodeList = coursesNode.ChildNodes;
                    if (courseNodeList != null)
                    {
                        foreach (XmlNode courseNode in courseNodeList)
                        {
                            Console.Write("\t");
                            Console.Write(courseNode.Attributes["name"].Value);
                            Console.Write("老師評語");
                            //通過FirstNode屬性可以獲得課程節(jié)點(diǎn)的第一個(gè)子節(jié)點(diǎn),LastNode可以獲得最后一個(gè)子節(jié)點(diǎn)
                            XmlNode teacherCommentNode = courseNode.FirstChild;
                            //讀取CData節(jié)點(diǎn)
                            XmlCDataSection cdata = (XmlCDataSection)teacherCommentNode.FirstChild;
                            Console.WriteLine(cdata.InnerText.Trim());
                        }
                    }
                }
            }

            Console.Write("\r\nPress any key to continue....");
            Console.Read();
        }
    }
}

XmlDocument本身是從XmlNode繼承的,讀Xml節(jié)點(diǎn)可以通過FirstChild,LastChild,或者NextSibling,PreviousSibling讀取單個(gè)節(jié)點(diǎn),或者通過ChildNodes讀取所有子節(jié)點(diǎn)。還可以使用XPath表達(dá)式使用SelectNodes(string xpath)或者SelectSingleNode(string xpath)讀取單個(gè)或者多個(gè)符合條件的節(jié)點(diǎn)。

2.如何通過XmlDocument編輯Xml

同樣是讀取Xml中的xml例子,我們這次要用csharp代碼生成xml,如下代碼:

/*玉開博客 http://www.php.cn/ */
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace WriteXml
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            //創(chuàng)建Xml聲明部分,即<?xml version="1.0" encoding="utf-8" ?>
            xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");

            //創(chuàng)建根節(jié)點(diǎn)
            XmlNode rootNode = xmlDoc.CreateElement("students");

            //創(chuàng)建student子節(jié)點(diǎn)
            XmlNode studentNode = xmlDoc.CreateElement("student");
            //創(chuàng)建一個(gè)屬性
            XmlAttribute nameAttribute = xmlDoc.CreateAttribute("name");
            nameAttribute .Value = "張同學(xué)";
            //xml節(jié)點(diǎn)附件屬性
            studentNode.Attributes.Append(nameAttribute);

           
            //創(chuàng)建courses子節(jié)點(diǎn)
            XmlNode coursesNode = xmlDoc.CreateElement("courses");
            XmlNode courseNode1 = xmlDoc.CreateElement("course");
            XmlAttribute courseNameAttr = xmlDoc.CreateAttribute("name");
            courseNameAttr.Value = "語文";
            courseNode1.Attributes.Append(courseNameAttr);
            XmlNode teacherCommentNode = xmlDoc.CreateElement("teacherComment");
            //創(chuàng)建Cdata塊
            XmlCDataSection cdata = xmlDoc.CreateCDataSection("<font color=\"red\">這是語文老師的批注</font>");
            teacherCommentNode.AppendChild(cdata);
            courseNode1.AppendChild(teacherCommentNode);
            coursesNode.AppendChild(courseNode1);
            //附加子節(jié)點(diǎn)
            studentNode.AppendChild(coursesNode);

            rootNode.AppendChild(studentNode);
            //附加根節(jié)點(diǎn)
            xmlDoc.AppendChild(rootNode);

            //保存Xml文檔
            xmlDoc.Save(@"d:\test.xml");

            Console.WriteLine("已保存Xml文檔");


        }
    }
}

使用XmlDocument生成xml的要點(diǎn)在于使用xmlDocument的實(shí)例的CreateElement創(chuàng)建XmlNode或者通過CreateAttribute方法創(chuàng)建屬性,并通過AppendChild方法附加xml節(jié)點(diǎn),通過AppendAttribute附加Attribute到節(jié)點(diǎn)的屬性集合。

關(guān)于使用XmlDocument怎么對Xml文檔進(jìn)行讀寫操作就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前名稱:使用XmlDocument怎么對Xml文檔進(jìn)行讀寫操作
網(wǎng)站URL:http://www.chinadenli.net/article2/joejic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)移動(dòng)網(wǎng)站建設(shè)云服務(wù)器商城網(wǎng)站Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)