下面使用C#來操作XML,注意這裡不使用DataSet來操作,DataSet操作確實很方便,但是如果讀取某一節點等等資料時就不太好了,所以本文介紹的是XmlDocument讀寫xml所有節點及讀取xml節點的資料,開始吧。
讀:
//開啟某檔案(假設web.config在根目錄中)
string filename=Server.MapPath("/") + @"WebApplication1\web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);
//得到頂層節點列表
XmlNodeList topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()=="appsettings")
{
//得到該節點的子節點
XmlNodeList nodelist=element.ChildNodes;
if ( nodelist.Count >0 )
{
//DropDownList1.Items.Clear();
foreach(XmlElement el in nodelist)//讀元素值
{
//DropDownList1.Items.Add(el.Attributes["key"].InnerXml);
//this.TextBox2.Text=el.Attributes["key"].InnerText;
this.TextBox2.Text=el.Attributes["key"].Value;
this.Label1.Text=el.Attributes["value"].Value;
//同樣在這裡可以修改元素值,在後面save。
// el.Attributes["value"].Value=this.TextBox2.Text;
}
}
}
}
xmldoc.Save(filename);
在某節點下增加一個元素,並設定值:
if(element.Name.ToLower()=="appsettings")
{
XmlElement elem =xmldoc.CreateElement("add");
element.AppendChild(elem);
elem.InnerText="ltp";
xmldoc.Save(filename);
}
效果:
<appSettings>
<add key="密碼" value="admin" />
<add>ltp</add>
</appSettings>
在某節點下增加一個元素,並增加兩個屬性:
if(element.Name.ToLower()=="appsettings")
{
XmlElement elem =xmldoc.CreateElement("add");
element.AppendChild(elem);
XmlAttribute xa=xmldoc.CreateAttribute("key");
xa.Value="ltp";
XmlAttribute xa2=xmldoc.CreateAttribute("value");
xa2.Value="first";
elem.SetAttributeNode(xa);
elem.SetAttributeNode(xa2);
xmldoc.Save(filename);
}
效果:
<appSettings>
<add key="密碼" value="admin" />
<add key="ltp" value="first" />
</appSettings>
//新增空元素:
XmlNode node=doc.CreateElement(groupname);
node.InnerText="";
doc.LastChild.AppendChild(node);
doc.Save(xmlfile);
刪除一個節點元素
string itemname=this.listBox1.SelectedItem.ToString();
this.listBox1.Items.Remove(this.listBox1.SelectedItem);
//begin del xmlfile
XmlDocument doc=new XmlDocument();
doc.Load(xmlfile);
XmlNodeList topM=doc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name==this.comboBox1.Text)
{
//得到該節點的子節點
XmlNodeList nodelist=element.ChildNodes;
foreach(XmlElement el in nodelist)//讀元素值
{
if(el.Attributes["key"].Value==itemname)
{
element.RemoveChild(el);
}
}//迴圈元素
}//得到組
}//迴圈組
doc.Save(xmlfile); //一定要儲存一下,否則不起作用
//篩選資料
private void Reader_Xml(string pathFlie)
{
XmlDocument Xmldoc=new XmlDocument();
Xmldoc.Load(pathFlie);
XmlNodeList Record1=Xmldoc.DocumentElement.SelectNodes(Code[@id='1'])
int f=0;
foreach(XmlNode xnode in Record1)
{
}
} /**//*讀取xml資料 兩種xml方式*/
<aaa>
<bb>something</bb>
<cc>something</cc>
</aaa>
<aaa>
<add key="123" value="321"/>
</aaa>
/**//*第一種方法*/
DS.ReadXml("your xmlfile name");
Container.DataItem("bb");
Container.DataItem("cc");
DS.ReadXmlSchema("your xmlfile name");
/**//*第二種方法*/
<aaa>
<add key="123" value="321"/>
</aaa>
如果我要找到123然後取到321應該怎麼寫呢?
using System.XML;
XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
xmlDoc.Load(@"c:\Config.xml");
XmlElement elem = xmlDoc.GetElementById("add");
string str = elem.Attributes["value"].Value
/**//*第三種方法: SelectSingleNode 讀取兩種格式的xml *---/
--------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<ConnectionString>Data Source=yf; user id=ctm_dbo;password=123</ConnectionString>
</appSettings>
</configuration>
--------------------------------------------------------------------------
XmlDocument doc = new XmlDocument();
doc.Load(strXmlName);
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/ConnectionString");
if(node!=null)
{
string k1=node.Value; //null
string k2=node.InnerText;//Data Source=yf; user id=ctm_dbo;password=123
string k3=node.InnerXml;//Data Source=yf; user id=ctm_dbo;password=123
node=null;
}
********************************************************************
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=yf; user id=ctm_dbo;password=123" />
</appSettings>
</configuration>
**--------------------------------------------------------------------**
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
string k=node.Attributes["key"].Value;
string v=node.Attributes["value"].Value;
node=null;
}
*--------------------------------------------------------------------*
XmlNode node=doc.SelectSingleNode("/configuration/appSettings/add");
if(node!=null)
{
XmlNodeReader nr=new XmlNodeReader(node);
nr.MoveToContent();
//檢查當前節點是否是內容節點。如果此節點不是內容節點,則讀取器向前跳至下一個內容節點或檔案結尾。
nr.MoveToAttribute("value");
string s=nr.Value;
node=null;
}
標籤:XmlDocument,xml所有節點
文章來自學IT網:http://www.xueit.com/html/2009-12-15/21-1376802495906.html
C#不使用DataSet操作XML,XmlDocument讀寫xml所有節點及讀取xml節點的資料總結
相關文章
- 使用C#讀寫xml檔案C#XML
- XML DOM 新增節點概述XML
- C#讀取Xml檔案C#XML
- XML DOM 替換節點概述XML
- XML DOM 刪除節點概述XML
- Unity 讀取xmlUnityXML
- 使用jaxp解析器dom方式對xml節點進行操作XML
- C# - XML讀寫與序列化C#XML
- 序列化篇 生成xml 以及讀取xmlXML
- Python xml.etree.ElementTree讀寫xml檔案例項PythonXML
- 利用xml輕鬆讀取web.config中的使用者自定義節XMLWeb
- XML文件節點導航與選擇指南XML
- XML節點自動生成簡單例項XML單例
- Java系列:讀取XML檔案JavaXML
- nodejs xmlreader 讀寫xml檔案NodeJSXML
- C# 操作xml(轉)C#XML
- 把list集合的內容寫入到Xml中,通過XmlDocument方式寫入Xml檔案中XML
- 資料庫——查詢樹形結構某節點的所有子節點、所有父節點資料庫
- DOM 節點遍歷:掌握遍歷 XML文件結構和內容的技巧XML
- [xmlbeans]自動生成讀寫xml的java類XMLBeanJava
- QDomDocument 讀取和編輯xml檔案XML
- dom4j 根據xml節點路徑查詢節點,找到對應的目標節點下的子節點,對節點Text值進行修改XML
- Jaxb - 生成帶名稱空間和節點字首的Xml的方式XML
- C# XML解析C#XML
- 【超好用API推薦】用dom4j建立XML檔案並寫入節點APIXML
- vue專案中使用bpmn-流程圖xml檔案中節點屬性轉json結構Vue流程圖XMLJSON
- JSON 與XML相比優點JSONXML
- PHP xml 轉陣列 陣列轉 xml 操作PHPXML陣列
- 使用FOR XML AUTO控制XML輸出KHXML
- 基於Xml 的IOC 容器-開始讀取配置內容XML
- App.config自定義節點讀取APP
- Android讀取XML實現軟體自動登陸AndroidXML
- C# 之 Linq to XmlC#XML
- QT: 操作主從檢視及XMLQTXML
- Oracle細節及難點總結Oracle
- 如何用python分析xml獲取資料?PythonXML
- Oracle RAC 第二節點打補丁報錯 oui-patch.xml (Permission denied)OracleUIXML
- Mybatis去xml化:我再也不想寫xml了MyBatisXML
- xml操作的幾種方法XML