读取XML文件中的指定值及属性(供大家分享)

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)
    {

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值