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)
{
}
}
读取XML文件中的指定值及属性(供大家分享)
最新推荐文章于 2023-09-01 15:18:38 发布