在C#中創(chuàng)建和讀取XML文件的實(shí)現(xiàn)方法
1.創(chuàng)建簡(jiǎn)單的XML文件
為了便于測(cè)試,我們首先創(chuàng)建控制臺(tái)應(yīng)用程序,項(xiàng)目命名為CreateXml,Program.cs代碼如下:
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)容為
<?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)單修改即可,修改如下:
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)容如下:
<?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
.Net6中加入了兩個(gè)新的時(shí)間類型:DateOnly和TimeOnly,但DateOnly和TimeOnly都有相應(yīng)的應(yīng)用場(chǎng)景,所以本文就來自定義一個(gè)時(shí)間類型YearMonth,用于解決實(shí)際項(xiàng)目開發(fā)中的需求,希望對(duì)大家有所幫助2023-07-07Unity實(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關(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-08C#?使用EntityFramework?CodeFirst?創(chuàng)建PostgreSQL數(shù)據(jù)庫的詳細(xì)過程
這篇文章主要介紹了C#使用EntityFramework?CodeFirst創(chuàng)建PostgreSQL數(shù)據(jù)庫的過程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07VS里使用C#制作窗口應(yīng)用的項(xiàng)目實(shí)踐
C#窗體的頻率使用特別高,本文主要介紹了VS里使用C#制作窗口應(yīng)用的項(xiàng)目實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02