讀:
//打開某文件(假設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;
}
http://www.cnblogs.com/skylaugh/archive/2006/12/18/595637.Html