欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在C#中創(chuàng)建和讀取XML文件的實(shí)現(xiàn)方法

 更新時(shí)間:2013年09月04日 08:35:03   作者:  
項(xiàng)目中需要將前臺(tái)頁面中的信息保存下來并存儲(chǔ)為xml文件格式到數(shù)據(jù)庫中去。因此我先在這里通過一個(gè)小實(shí)例來學(xué)習(xí)xml的創(chuàng)建與讀取

1.創(chuàng)建簡(jiǎn)單的XML文件
為了便于測(cè)試,我們首先創(chuàng)建控制臺(tái)應(yīng)用程序,項(xiàng)目命名為CreateXml,Program.cs代碼如下:

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CreateXml
{
    class Program
    {
        static void Main(string[] args)
        {
            Program app = new Program();
            app.CreateXmlFile();         
        }
        public void CreateXmlFile()
        {
            XmlDocument xmlDoc = new XmlDocument();
            //創(chuàng)建類型聲明節(jié)點(diǎn)
            XmlNode node=xmlDoc.CreateXmlDeclaration("1.0","utf-8","");
            xmlDoc.AppendChild(node);
            //創(chuàng)建根節(jié)點(diǎn)
            XmlNode root = xmlDoc.CreateElement("User");
            xmlDoc.AppendChild(root);
            CreateNode(xmlDoc, root, "name", "xuwei");
            CreateNode(xmlDoc, root, "sex", "male");
            CreateNode(xmlDoc, root, "age", "25");
            try
            {
                xmlDoc.Save("c://data2.xml");
            }
            catch (Exception e)
            {
                //顯示錯(cuò)誤信息
                Console.WriteLine(e.Message);
            }
            //Console.ReadLine();

        }

        /// <summary> 
        /// 創(chuàng)建節(jié)點(diǎn) 
        /// </summary> 
        /// <param name="xmldoc"></param>  xml文檔
        /// <param name="parentnode"></param>父節(jié)點(diǎn) 
        /// <param name="name"></param>  節(jié)點(diǎn)名
        /// <param name="value"></param>  節(jié)點(diǎn)值
        ///
        public void CreateNode(XmlDocument xmlDoc,XmlNode parentNode,string name,string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, null);
            node.InnerText = value;
            parentNode.AppendChild(node);
        }
    } 
}


這樣會(huì)在C盤根目錄下創(chuàng)建data2.xml文件,文件內(nèi)容為
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<User>
  <name>xuwei</name>
  <sex>male</sex>
  <age>25</age>
</User>

2.創(chuàng)建多節(jié)點(diǎn)多層級(jí)的XML文件
只需要對(duì)CreateXmlFile()方法進(jìn)行簡(jiǎn)單修改即可,修改如下:
復(fù)制代碼 代碼如下:

public void CreateXmlFile()
        {
            XmlDocument xmlDoc = new XmlDocument();
            //創(chuàng)建類型聲明節(jié)點(diǎn)
            XmlNode node=xmlDoc.CreateXmlDeclaration("1.0","utf-8","");
            xmlDoc.AppendChild(node);
            //創(chuàng)建根節(jié)點(diǎn)
            XmlNode root = xmlDoc.CreateElement("Users");
            xmlDoc.AppendChild(root);

            XmlNode node1 = xmlDoc.CreateNode(XmlNodeType.Element, "User", null);
            CreateNode(xmlDoc, node1, "name", "xuwei");
            CreateNode(xmlDoc, node1, "sex", "male");
            CreateNode(xmlDoc, node1, "age", "25");
            root.AppendChild(node1);

            XmlNode node2 = xmlDoc.CreateNode(XmlNodeType.Element, "User", null);
            CreateNode(xmlDoc, node2, "name", "xiaolai");
            CreateNode(xmlDoc, node2, "sex", "female");
            CreateNode(xmlDoc, node2, "age", "23");
            root.AppendChild(node2);

            try
            {
                xmlDoc.Save("c://data5.xml");
            }
            catch (Exception e)
            {
                //顯示錯(cuò)誤信息
                Console.WriteLine(e.Message);
            }
            //Console.ReadLine();

        }


生成的xml文件內(nèi)容如下:
復(fù)制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<Users>
  <User>
    <name>xuwei</name>
    <sex>male</sex>
    <age>25</age>
  </User>
  <User>
    <name>xiaolai</name>
    <sex>female</sex>
    <age>23</age>
  </User>
</Users>

相關(guān)文章

  • 利用C#自定義一個(gè)時(shí)間類型YearMonth

    利用C#自定義一個(gè)時(shí)間類型YearMonth

    .Net6中加入了兩個(gè)新的時(shí)間類型:DateOnly和TimeOnly,但DateOnly和TimeOnly都有相應(yīng)的應(yīng)用場(chǎng)景,所以本文就來自定義一個(gè)時(shí)間類型YearMonth,用于解決實(shí)際項(xiàng)目開發(fā)中的需求,希望對(duì)大家有所幫助
    2023-07-07
  • C#迷你猜數(shù)實(shí)例分析

    C#迷你猜數(shù)實(shí)例分析

    這篇文章主要介紹了C#迷你猜數(shù),實(shí)例分析C#操作數(shù)字及數(shù)組的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-03-03
  • Unity實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度

    Unity實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)已知落點(diǎn)和速度自動(dòng)計(jì)算發(fā)射角度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • C#接口實(shí)現(xiàn)方法實(shí)例分析

    C#接口實(shí)現(xiàn)方法實(shí)例分析

    這篇文章主要介紹了C#接口實(shí)現(xiàn)方法,以實(shí)例形式較為詳細(xì)的分析了C#接口的功能、定義及實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • 關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法詳解

    關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法詳解

    這篇文章主要給大家介紹了關(guān)于C#操作文件路徑(Directory)的常用靜態(tài)方法,Directory類位于System.IO 命名空間,Directory類提供了在目錄和子目錄中進(jìn)行創(chuàng)建移動(dòng)和列舉操作的靜態(tài)方法,需要的朋友可以參考下
    2021-08-08
  • C#實(shí)現(xiàn)過濾sql特殊字符的方法集合

    C#實(shí)現(xiàn)過濾sql特殊字符的方法集合

    這篇文章主要介紹了C#實(shí)現(xiàn)過濾sql特殊字符的方法,以實(shí)例形式分析總結(jié)了C#針對(duì)SQL危險(xiǎn)字符的幾種常用的過濾技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-11-11
  • C#?使用EntityFramework?CodeFirst?創(chuàng)建PostgreSQL數(shù)據(jù)庫的詳細(xì)過程

    C#?使用EntityFramework?CodeFirst?創(chuàng)建PostgreSQL數(shù)據(jù)庫的詳細(xì)過程

    這篇文章主要介紹了C#使用EntityFramework?CodeFirst創(chuàng)建PostgreSQL數(shù)據(jù)庫的過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-07-07
  • 淺談C#中的委托、事件與異步

    淺談C#中的委托、事件與異步

    本文主要介紹了C#中的委托、事件與異步的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來看下吧
    2017-03-03
  • VS里使用C#制作窗口應(yīng)用的項(xiàng)目實(shí)踐

    VS里使用C#制作窗口應(yīng)用的項(xiàng)目實(shí)踐

    C#窗體的頻率使用特別高,本文主要介紹了VS里使用C#制作窗口應(yīng)用的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-02-02
  • 深入解析C#中的abstract抽象類

    深入解析C#中的abstract抽象類

    這篇文章主要介紹了深入解析C#中的abstract抽象類,包括定義抽象類等C#面相對(duì)象編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2016-01-01

最新評(píng)論