DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 在C#.net中如何操作XML - asp.net
在C#.net中如何操作XML - asp.net
編輯:XML詳解     

在C#.Net中如何操作XML
需要添加的命名空間:
using System.XML;

定義幾個公共對象:
XmlDocument XMLdoc ;
XmlNode XMLnode ;
XmlElement XMLelem ;

1,創建到服務器同名目錄下的XML文件:

方法一:
xmldoc = new XMLDocument ( ) ;
//加入XML的聲明段落
xmlnode = xmldoc.CreateNode ( XmlNodeType.XMLDeclaration , "" , "" ) ;
xmldoc.AppendChild ( XMLnode ) ;
//加入一個根元素
xmlelem = XMLdoc.CreateElement ( "" , "Employees" , "" ) ;
xmldoc.AppendChild ( XMLelem ) ;
//加入另外一個元素
for(int i=1;i<3;i++)
{

XmlNode root=XMLdoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=XMLdoc.CreateElement("Node");//創建一個<Node>節點
xe1.SetAttribute("genre","李贊紅");//設置該節點genre屬性
xe1.SetAttribute("ISBN","2-3631-4");//設置該節點ISBN屬性

XmlElement xesub1=XMLdoc.CreateElement("title");
xesub1.InnerText="CS從入門到精通";//設置文本節點
xe1.AppendChild(xesub1);//添加到<Node>節點中
XmlElement xesub2=XMLdoc.CreateElement("author");
xesub2.InnerText="候捷";
xe1.AppendChild(xesub2);
XmlElement xesub3=XMLdoc.CreateElement("price");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);

root.AppendChild(xe1);//添加到<Employees>節點中
}
//保存創建好的XML文檔
xmldoc.Save ( Server.MapPath("data.XML") ) ;

//////////////////////////////////////////////////////////////////////////////////////
結果:在同名目錄下生成了名為data.XML的文件,內容如下,
<?XML version="1.0"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
</Employees>

方法二:
XmlTextWriter XMLWriter;
   string strFilename = Server.MapPath("data1.XML") ;

   xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//創建一個XML文檔
   XMLWriter.Formatting = Formatting.Indented;
   XMLWriter.WriteStartDocument();
   XMLWriter.WriteStartElement("Employees");

   XMLWriter.WriteStartElement("Node");
   XMLWriter.WriteAttributeString("genre","李贊紅");
   XMLWriter.WriteAttributeString("ISBN","2-3631-4");

   XMLWriter.WriteStartElement("title");
   XMLWriter.WriteString("CS從入門到精通");
   XMLWriter.WriteEndElement();

   XMLWriter.WriteStartElement("author");
   XMLWriter.WriteString("候捷");
   XMLWriter.WriteEndElement();

   XMLWriter.WriteStartElement("price");
   XMLWriter.WriteString("58.3");
   XMLWriter.WriteEndElement();

   XMLWriter.WriteEndElement();

   XMLWriter.Close();
//////////////////////////////////////////////////////////////////////////////////////
結果:
<?XML version="1.0" encoding="gb2312"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
</Employees>

2,添加一個結點:

XmlDocument xmlDoc=new XMLDocument();
xmlDoc.Load(Server.MapPath("data.XML"));
XmlNode root=XMLDoc.SelectSingleNode("Employees");//查找<Employees>
XmlElement xe1=XMLDoc.CreateElement("Node");//創建一個<Node>節點
xe1.SetAttribute("genre","張三");//設置該節點genre屬性
xe1.SetAttribute("ISBN","1-1111-1");//設置該節點ISBN屬性

XmlElement xesub1=XMLDoc.CreateElement("title");
xesub1.InnerText="C#入門幫助";//設置文本節點
xe1.AppendChild(xesub1);//添加到<Node>節點中
XmlElement xesub2=XMLDoc.CreateElement("author");
xesub2.InnerText="高手";
xe1.AppendChild(xesub2);
XmlElement xesub3=XMLDoc.CreateElement("price");
xesub3.InnerText="158.3";
xe1.AppendChild(xesub3);

root.AppendChild(xe1);//添加到<Employees>節點中
xmlDoc.Save ( Server.MapPath("data.XML") );

//////////////////////////////////////////////////////////////////////////////////////
結果:在XML原有的內容裡添加了一個結點,內容如下,
<?XML version="1.0"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
</Node>
  <Node genre="張三" ISBN="1-1111-1">
    <title>C#入門幫助</title>
    <author>高手</author>
    <price>158.3</price>
  </Node>
</Employees>

3,修改結點的值(屬性和子結點):

XmlDocument xmlDoc=new XMLDocument();
xmlDoc.Load( Server.MapPath("data.XML") );

XmlNodeList nodeList=XMLDoc.SelectSingleNode("Employees").ChildNodes;//獲取Employees節點的所有子節點

foreach(XMLNode xn in nodeList)//遍歷所有子節點
{
XmlElement xe=(XmlElement)xn;//將子節點類型轉換為XMLElement類型
if(xe.GetAttribute("genre")=="張三")//如果genre屬性值為“張三”
{
xe.SetAttribute("genre","update張三");//則修改該屬性為“update張三”

XMLNodeList nls=xe.ChildNodes;//繼續獲取xe子節點的所有子節點
foreach(XMLNode xn1 in nls)//遍歷
{
XmlElement xe2=(XMLElement)xn1;//轉換類型
if(xe2.Name=="author")//如果找到
{
xe2.InnerText="亞勝";//則修改
}
}
}
}
xmlDoc.Save( Server.MapPath("data.XML") );//保存。

//////////////////////////////////////////////////////////////////////////////////////
結果:將原來的所有結點的信息都修改了,XML的內容如下,
<?XML version="1.0"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="update張三" ISBN="1-1111-1">
    <title>C#入門幫助</title>
    <author>亞勝</author>
    <price>158.3</price>
  </Node>
</Employees>

4,修改結點(添加結點的屬性和添加結點的自結點):
XmlDocument xmlDoc=new XMLDocument();
xmlDoc.Load( Server.MapPath("data.XML") );

XmlNodeList nodeList=XMLDoc.SelectSingleNode("Employees").ChildNodes;//獲取Employees節點的所有子節點

foreach(XMLNode xn in nodeList)
{
XmlElement xe=(XMLElement)xn;
xe.SetAttribute("test","111111");

XmlElement xesub=XMLDoc.CreateElement("flag");
xesub.InnerText="1";
xe.AppendChild(xesub);
}
xmlDoc.Save( Server.MapPath("data.XML") );

//////////////////////////////////////////////////////////////////////////////////////
結果:每個結點的屬性都添加了一個,子結點也添加了一個,內容如下,
<?XML version="1.0"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4" test="111111">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
    <flag>1</flag>
  </Node>
  <Node genre="李贊紅" ISBN="2-3631-4" test="111111">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
    <flag>1</flag>
  </Node>
  <Node genre="update張三" ISBN="1-1111-1" test="111111">
    <title>C#入門幫助</title>
    <author>亞勝</author>
    <price>158.3</price>
    <flag>1</flag>
  </Node>
</Employees>

5,刪除結點中的某一個屬性:
XmlDocument xmlDoc=new XMLDocument();
xmlDoc.Load( Server.MapPath("data.XML") );
XmlNodeList xnl=XMLDoc.SelectSingleNode("Employees").ChildNodes;
foreach(XMLNode xn in xnl)
{
XmlElement xe=(XMLElement)xn;
xe.RemoveAttribute("genre");//刪除genre屬性

XMLNodeList nls=xe.ChildNodes;//繼續獲取xe子節點的所有子節點
foreach(XMLNode xn1 in nls)//遍歷
{
XmlElement xe2=(XMLElement)xn1;//轉換類型
if(xe2.Name=="flag")//如果找到
{
xe.RemoveChild(xe2);//則刪除
}
}
}
xmlDoc.Save( Server.MapPath("data.XML") );

//////////////////////////////////////////////////////////////////////////////////////]
結果:刪除了結點的一個屬性和結點的一個子結點,內容如下,
<?XML version="1.0"?>
<Employees>
  <Node ISBN="2-3631-4" test="111111">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node ISBN="2-3631-4" test="111111">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node ISBN="1-1111-1" test="111111">
    <title>C#入門幫助</title>
    <author>亞勝</author>
    <price>158.3</price>
</Node>
</Employees>

6,刪除結點:
XmlDocument xmlDoc=new XMLDocument();
xmlDoc.Load( Server.MapPath("data.XML") );
XmlNode root=XMLDoc.SelectSingleNode("Employees");
XmlNodeList xnl=XMLDoc.SelectSingleNode("Employees").ChildNodes;
for(int i=0;i<xnl.Count;i++)
{
XmlElement xe=(XMLElement)xnl.Item(i);
if(xe.GetAttribute("genre")=="張三")
{
root.RemoveChild(xe);
if(i<xnl.Count)i=i-1;
}
}
xmlDoc.Save( Server.MapPath("data.XML") );

//////////////////////////////////////////////////////////////////////////////////////]
結果:刪除了符合條件的所有結點,原來的內容:

<?XML version="1.0"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="張三" ISBN="1-1111-1">
    <title>C#入門幫助</title>
    <author>高手</author>
    <price>158.3</price>
  </Node>
  <Node genre="張三" ISBN="1-1111-1">
    <title>C#入門幫助</title>
    <author>高手</author>
    <price>158.3</price>
  </Node>
</Employees>

刪除後的內容:
<?XML version="1.0"?>
<Employees>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
  <Node genre="李贊紅" ISBN="2-3631-4">
    <title>CS從入門到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </Node>
</Employees>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved