读:
}
}
}
xmldoc.Save(filename);
//打开某文件(假设web.config在根目录中)
string filename=Server.MapPath("/") + @"WebApplication1/web.config";
XmlDocument xmldoc= new XmlDocument();
xmldoc.Load(filename);
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 topM=xmldoc.DocumentElement.ChildNodes;
foreach(XmlElement element in topM)
{
if(element.Name.ToLower()=="appsettings")
{
//得到该节点的子节点
XmlNodeList nodelist=element.ChildNodes;
XmlNodeList nodelist=element.ChildNodes;
if ( nodelist.Count >0 )
{
//DropDownList1.Items.Clear();
{
//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;
{
//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);
}效果:
ltp
在某节点下增加一个元素,并增加两个属性: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);
}效果:
//添加空元素: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)
{}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=104334
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=549713