手机站
网通分站
电信主站
密 码:
用户名:
当前位置 : 主页>网络编程>Xml编程>列表

读写xml所有节点个人小结和读取xml节点的数据总结

来源:互联网 作者:west263.com 时间:2008-02-23
西部数码-全国虚拟主机10强!40余项虚拟主机管理功能,全国领先!双线多线虚拟主机南北访问畅通无阻!免费赠送企业邮局,.CN域名,自助建站480元起,免费试用7天,满意再付款! P4主机租用799元/月.月付免压金!

读:
//打开某文件(假设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

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!