伸进女同桌乳沟里摸爽了,小雪解开乳罩给老杨摸,少妇被按摩师摸高潮了,亲嘴扒胸摸屁股激烈视频,强摸秘书人妻大乳BD,摸BBB揉BBB揉BBB视频

新疆軟件開發(fā)

本站首頁 軟件開發(fā) 成功案例 公司新聞 公司簡介 客服中心 軟件技術 網(wǎng)站建設
  您現(xiàn)在的位置: 新疆二域軟件開發(fā)公司 >> .Net技術 >> 文章正文

實用技術:如何在C#.net中操作使用XML

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

定義幾個公共對象:
XmlDocument xmldoc ;
XmlNode xmlnode ;
XmlElement xmlelem ;

1,創(chuàng)建到服務器同名目錄下的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");//創(chuàng)建一個<Node>節(jié)點 
xe1.SetAttribute("genre","李贊紅");//設置該節(jié)點genre屬性 
xe1.SetAttribute("ISBN","2-3631-4");//設置該節(jié)點ISBN屬性 

XmlElement xesub1=xmldoc.CreateElement("title"); 
xesub1.InnerText="CS從入門到精通";//設置文本節(jié)點 
xe1.AppendChild(xesub1);//添加到<Node>節(jié)點中 
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>節(jié)點中 
}
//保存創(chuàng)建好的XML文檔
xmldoc.Save ( Server.MapPath("data.xml") ) ; 

//////////////////////////////////////////////////////////////////////////////////////
結(jié)果:在同名目錄下生成了名為data.xml的文件,內(nèi)容如下,
<?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);//創(chuàng)建一個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();
//////////////////////////////////////////////////////////////////////////////////////
結(jié)果:
<?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,添加一個結(jié)點:

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

XmlElement xesub1=xmlDoc.CreateElement("title"); 
xesub1.InnerText="C#入門幫助";//設置文本節(jié)點 
xe1.AppendChild(xesub1);//添加到<Node>節(jié)點中 
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>節(jié)點中 
xmlDoc.Save ( Server.MapPath("data.xml") );

//////////////////////////////////////////////////////////////////////////////////////
結(jié)果:在xml原有的內(nèi)容里添加了一個結(jié)點,內(nèi)容如下,
<?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,修改結(jié)點的值(屬性和子結(jié)點):

XmlDocument xmlDoc=new XmlDocument();

xmlDoc.Load( Server.MapPath("data.xml") ); 

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

foreach(XmlNode xn in nodeList)//遍歷所有子節(jié)點 

XmlElement xe=(XmlElement)xn;//將子節(jié)點類型轉(zhuǎn)換為XmlElement類型 
if(xe.GetAttribute("genre")=="張三")//如果genre屬性值為“張三”


xe.SetAttribute("genre","update張三");//則修改該屬性為“update張三” 

 

XmlNodeList nls=xe.ChildNodes;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點 
foreach(XmlNode xn1 in nls)//遍歷 

XmlElement xe2=(XmlElement)xn1;//轉(zhuǎn)換類型 
if(xe2.Name=="author")//如果找到 

xe2.InnerText="亞勝";//則修改




xmlDoc.Save( Server.MapPath("data.xml") );//保存。

//////////////////////////////////////////////////////////////////////////////////////
結(jié)果:將原來的所有結(jié)點的信息都修改了,xml的內(nèi)容如下,
<?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,修改結(jié)點(添加結(jié)點的屬性和添加結(jié)點的自結(jié)點):
XmlDocument xmlDoc=new XmlDocument(); 
xmlDoc.Load( Server.MapPath("data.xml") ); 

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

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") );

//////////////////////////////////////////////////////////////////////////////////////
結(jié)果:每個結(jié)點的屬性都添加了一個,子結(jié)點也添加了一個,內(nèi)容如下,
<?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,刪除結(jié)點中的某一個屬性:

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;//繼續(xù)獲取xe子節(jié)點的所有子節(jié)點 
foreach(XmlNode xn1 in nls)//遍歷 

XmlElement xe2=(XmlElement)xn1;//轉(zhuǎn)換類型 
if(xe2.Name=="flag")//如果找到 

xe.RemoveChild(xe2);//則刪除



xmlDoc.Save( Server.MapPath("data.xml") ); 

//////////////////////////////////////////////////////////////////////////////////////]
結(jié)果:刪除了結(jié)點的一個屬性和結(jié)點的一個子結(jié)點,內(nèi)容如下,
<?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,刪除結(jié)點:
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") ); 

//////////////////////////////////////////////////////////////////////////////////////]
結(jié)果:刪除了符合條件的所有結(jié)點,原來的內(nèi)容:

<?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> 

 

刪除后的內(nèi)容:
<?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>

作者:未知 | 文章來源:未知 | 更新時間:2007-12-25 13:04:17

  • 上一篇文章:

  • 下一篇文章:

  • 相關文章:
    怎么將樹輸出到XML文件代碼?
    介紹XML在Web頁面中的應用
    XML和XSLT結(jié)合使你的網(wǎng)站設計渾然一體
    軟件技術
    · 開發(fā)語言
    · Java技術
    · .Net技術
    · 數(shù)據(jù)庫開發(fā)
    最新文章  
    ·.net技術 asp.net MVC3 Vi
    ·.net將視頻文件格式轉(zhuǎn)換成
    ·NET Framework 2.0 Servic
    ·如何動態(tài)修改winform的app
    ·asp,net軟件結(jié)構(gòu)設計和相關
    ·學習怎樣使用ASP.NET中的虛
    ·,net基礎類的學習:system
    ·.net學習:c#事件的深入分
    ·Facade Pattern學習總結(jié)
    ·C#如何設置或者獲取目錄的
    ·如何使用XSL來定義ASP.NET
    ·理解WCF Data Contract契約
    ·如何能在.net2.0開發(fā)的控件
    ·模式怎樣使用,講解模式的
    ·如何解決Menu菜單被frame遮
    關于我們 | 軟件開發(fā) | 下載試用 | 客服中心 | 聯(lián)系我們 | 友情鏈接 | 網(wǎng)站地圖 | 新疆電子地圖 | RSS訂閱
    版權所有 © 2016 新疆二域軟件開發(fā)網(wǎng) m.zgna.net All Rights Reserved 新ICP備14003571號
    新疆軟件開發(fā)總機:0991-4842803、4811639.
    客服QQ:596589785 ;地址:新疆烏魯木齊北京中路華聯(lián)大廈A-5C 郵編:830000
     
    黄冈市| 安丘市| 海口市| 平潭县| 武宁县| 墨竹工卡县| 将乐县| 松阳县| 乌苏市| 吉隆县| 从江县| 青海省| 海城市| 惠来县| 黎川县| 平塘县| 钟山县| 石河子市| 汉寿县| 东海县| 霍州市| 泗阳县| 镇安县| 镇远县| 海门市| 宣化县| 大新县| 苗栗县| 屯昌县| 乌鲁木齐县| 宜阳县| 彭阳县| 朝阳市| 柳林县| 东丽区| 缙云县| 休宁县| 斗六市| 大化| 孟津县| 平乡县|