读取XML文件中的指定值及属性(供大家分享)
2011-05-26 11:12
211 查看
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
//using System.IO;
using System.Xml.XPath;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArg*path查询示例
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("App_Data/j2ee.xml"));
//获取该节点的父节点属性值以及该节点父节点上一级的属性值
TextBox1.Text=node.ParentNode.Attributes[0].Value;
TextBox1.Text=node.ParentNode.ParentNode.Attributes[0].Value;
//根据绝对路径查找节点
//XmlNodeList list = doc.SelectNodes("/shop/book/author");
//XmlNode node= list.Item(0);
//TextBox1.Text = node.InnerText;
// xpath条件查询
//XmlNodeList list = doc.SelectNodes("/shop/book[price>10]/title");
//TextBox1.Text = list.Item(0).InnerText;
//根据属性查找[]表示属性查询
//XmlNodeList list = doc.SelectNodes("//price[@unit='¥']");
//TextBox1.Text = list.Item(0).InnerText;
//XmlNodeList list = doc.SelectNodes("//price[@unit]");
//TextBox1.Text = list.Item(0).InnerText;
//XmlNodeList list = doc.SelectNodes("//price[@*]");
//TextBox1.Text = list.Item(0).InnerText;
//列出所有的属性
//XmlNodeList list = doc.SelectNodes("/shop/book[0]");
// foreach (XmlNode node in list)
// TextBox1.Text += node.Name + "=" + node.Value;
//XmlAttribute node =(XmlAttribute)doc.SelectNodes("//@*");
//node.OwnerElement.Attributes.Remove(node);
//node.ParentNode.Attributes.Remove(node);
//XmlNodeList list = doc.SelectNodes("//author");
//foreach (XmlNode node in list)
//{
// if (node.InnerText.StartsWith("zhang"))
// node.Value = node.Value.Replace("zhang", "li");
//}
//XmlNodeList list = doc.SelectNodes("//@*");
//foreach (XmlNode node in list)
//{
// XmlAttribute attribute = (XmlAttribute)node;
// if(attribute.Value.Equals("RMB"))
// attribute.OwnerElement.Attributes.Remove(attribute);
//}
//XmlNodeList list = doc.SelectNodes("//@*");
//foreach (XmlNode node in list)
//{
// XmlAttribute attribute = (XmlAttribute)node;
// //if (attribute.Value.Equals("RMB"))
// attribute.OwnerElement.Attributes.Remove(attribute);
//}
XmlNodeList list = doc.SelectNodes("/shop");
XmlElement add=doc.CreateElement("mark");
add.InnerText="hello";
list.Item(0).InsertAfter(add, list.Item(0).ChildNodes[1]);
doc.Save(Server.MapPath("App_Data/j2ee.xml"));
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
//using System.IO;
using System.Xml.XPath;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArg*path查询示例
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("App_Data/j2ee.xml"));
//获取该节点的父节点属性值以及该节点父节点上一级的属性值
TextBox1.Text=node.ParentNode.Attributes[0].Value;
TextBox1.Text=node.ParentNode.ParentNode.Attributes[0].Value;
//根据绝对路径查找节点
//XmlNodeList list = doc.SelectNodes("/shop/book/author");
//XmlNode node= list.Item(0);
//TextBox1.Text = node.InnerText;
// xpath条件查询
//XmlNodeList list = doc.SelectNodes("/shop/book[price>10]/title");
//TextBox1.Text = list.Item(0).InnerText;
//根据属性查找[]表示属性查询
//XmlNodeList list = doc.SelectNodes("//price[@unit='¥']");
//TextBox1.Text = list.Item(0).InnerText;
//XmlNodeList list = doc.SelectNodes("//price[@unit]");
//TextBox1.Text = list.Item(0).InnerText;
//XmlNodeList list = doc.SelectNodes("//price[@*]");
//TextBox1.Text = list.Item(0).InnerText;
//列出所有的属性
//XmlNodeList list = doc.SelectNodes("/shop/book[0]");
// foreach (XmlNode node in list)
// TextBox1.Text += node.Name + "=" + node.Value;
//XmlAttribute node =(XmlAttribute)doc.SelectNodes("//@*");
//node.OwnerElement.Attributes.Remove(node);
//node.ParentNode.Attributes.Remove(node);
//XmlNodeList list = doc.SelectNodes("//author");
//foreach (XmlNode node in list)
//{
// if (node.InnerText.StartsWith("zhang"))
// node.Value = node.Value.Replace("zhang", "li");
//}
//XmlNodeList list = doc.SelectNodes("//@*");
//foreach (XmlNode node in list)
//{
// XmlAttribute attribute = (XmlAttribute)node;
// if(attribute.Value.Equals("RMB"))
// attribute.OwnerElement.Attributes.Remove(attribute);
//}
//XmlNodeList list = doc.SelectNodes("//@*");
//foreach (XmlNode node in list)
//{
// XmlAttribute attribute = (XmlAttribute)node;
// //if (attribute.Value.Equals("RMB"))
// attribute.OwnerElement.Attributes.Remove(attribute);
//}
XmlNodeList list = doc.SelectNodes("/shop");
XmlElement add=doc.CreateElement("mark");
add.InnerText="hello";
list.Item(0).InsertAfter(add, list.Item(0).ChildNodes[1]);
doc.Save(Server.MapPath("App_Data/j2ee.xml"));
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
}
相关文章推荐
- 创建xml文件,添加节点,读取xml文件中某属性的属性值
- 【个人学习笔记】xml 文件的属性读取
- java按指定编码写入和读取文件内容的类分享
- spring xml配置文件中指定的xsd文件读取不到了org.xml.sax.SAXParseException:
- System.Security.Cryptography.RSA.FromXmlString 系统找不到指定的文件和X509读取证书文件系统找不到指定的文件异常
- 利用LIBXML库遍历读取XML文件所有的属性和content
- SpringBoot内部配置:“application.properties配置”和”使用XML配置”,读取属性文件中的内容,日志配置,Profile配置(学习:SpringBoot实战)
- 给大家分享一下项目中基于springMVC基于注解部分的xml配置文件,很实用
- Unity3D 读取XML文件里面的属性
- MyEclipse10在读取XML文件中的数据找不到指定路径
- 分享下java 读取目录及子目录下指定文件名的路径 并放到一个List数组里面返回遍历和写到文件里
- XMl 文件属性的读取
- Spring —— web.xml 中用 PropertyPlaceholderConfigurer 类读取properties 文件 或 属性
- 分享非常有用的Java程序 (关键代码)(六)---解析/读取XML 文件(重要)
- 分享非常有用的Java程序 (关键代码)(六)---解析/读取XML 文件(重要)
- 跟大家分享下Android布局文件layout.xml的一些属性值。
- 使用PropertyPlaceholderConfigurer类读取外部属性文件内容到XML
- 读取XML文件中的某个节点的某个属性
- spring的xml配置文件读取properties文件中的属性
- C#2005用XML来保存连接数据库的字符串(这样改变了服务器连接配制只需改一下这个文件就成了),琢磨了几天终于有解了,和大家分享一下,希望高手指点!