读写xml所有节点个人小结 和 读取xml节点的数据总结
2005-12-03 17:58
951 查看
读:
//打开某文件(假设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)
xmldoc.Save(filename);
在某节点下增加一个元素,并设置值:
if(element.Name.ToLower()=="appsettings")
效果:
<appSettings>
<add key="密码" value="admin" />
<add>ltp</add>
</appSettings>
在某节点下增加一个元素,并增加两个属性:
if(element.Name.ToLower()=="appsettings")
效果:
<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)
doc.Save(xmlfile); //一定要保存一下,否则不起作用
//筛选数据
private void Reader_Xml(string pathFlie)
<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;
}
//打开某文件(假设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)
xmldoc.Save(filename);
在某节点下增加一个元素,并设置值:
if(element.Name.ToLower()=="appsettings")
效果:
<appSettings>
<add key="密码" value="admin" />
<add>ltp</add>
</appSettings>
在某节点下增加一个元素,并增加两个属性:
if(element.Name.ToLower()=="appsettings")
效果:
<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)
doc.Save(xmlfile); //一定要保存一下,否则不起作用
//筛选数据
private void Reader_Xml(string pathFlie)
<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;
}
相关文章推荐
- 【转】读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结和读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据总结
- 读写xml所有节点个人小结 和 读取xml节点的数据
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C#.NET示例读写xml所有节点的代码实现方法和读取xml节点的数据总结
- C#不使用DataSet操作XML,XmlDocument读写xml所有节点及读取xml节点的数据总结
- 读写xml所有节点个人小结
- 读写xml所有节点个人小结
- 读写xml所有节点个人小结
- 读写xml所有节点个人小结