• asp.net怎么读取或保存xml文件里的数据范例


    更多范例见
    XML读 写 简单留言本 http://hi.baidu.com/wenjunlin/blog/item/866585cedfbbd90892457e8e.html
    生成XML文件 http://hi.baidu.com/wenjunlin/blog/item/2c49b103e8918be708fa93a7.html
    XML的增 删 改 查询操作 http://hi.baidu.com/wenjunlin/blog/item/e64abb1fbeb1bc01314e154e.html
    <root>
    <env>
    <param name="rate" value="3" />
    <param name="url" value="3" />
    </env>
    <vr startpano="start" width="10" height="540" x="0" y="0" >
    <pano name="url" url="Car/Model/InteriorPanorama/BJXD_i30/S-max.jpg">
    </pano>
    </vr>
    <background file="control/bg.jpg">
    </background>
    </root>


    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;

    public partial class SomePages_OperateXML : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    string str = ReadXML();
    lblReadXML.Text = str;
    SaveXML();
    }
    private string ReadXML()
    {
    string strReturn = string.Empty;

    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("~/images/config.xml"));
    XmlElement root = doc.DocumentElement;

    //定义要求匹配项 name="url"
    string path = @"//*[@name='url']";
    //在当前节点根据匹配项进行寻找
    foreach (XmlNode node in root.SelectNodes(path))
    {
    strReturn += "匹配项:" + path;
    strReturn += "节点:" + node.Name;
    strReturn += "<br/>";
    }

    //直接寻找指定节点读取值
    XmlElement x = (XmlElement)root.SelectSingleNode("vr");
    if (x != null)
    {
    strReturn += x.GetAttribute("startpano");
    }
    return strReturn;

    }
    private void SaveXML()
    {
    XmlDocument doc = new XmlDocument();
    doc.Load(Server.MapPath("~/images/config.xml"));

    XmlElement root = doc.DocumentElement;
    XmlElement x = (XmlElement)root.SelectSingleNode("vr");

    if (x != null)
    {
    //设置节点属性
    x.SetAttribute("width1", "10");
    //创建节点
    XmlElement x1 = doc.CreateElement("test");
    x1.SetAttribute("width", "100");
    //添加节点
    x.AppendChild(x1);
    //保存xml
    doc.Save(Server.MapPath("~/images/config.xml"));
    }
    }
    }

    如何在 .NET Framework SDK 中使用 XmlDocument 类修改和保存 XML

    参考 http://support.microsoft.com/kb/301233
  • 相关阅读:
    [bzoj4417] [洛谷P3990] [Shoi2013] 超级跳马
    [bzoj4011] [洛谷P3244] [HNOI2015] 落忆枫音
    [bzoj1875] [洛谷P2151] [SDOI2009] HH去散步
    [bzoj4827] [洛谷P3723] [Hnoi2017] 礼物
    [bzoj2326] [洛谷P3216] [HNOI2011] 数学作业
    [bzoj3105] [cqoi2013] 新Nim游戏
    [YTU]_2353 ( 长方柱类【C++ 类定义】)
    [YTU]_2627 (职工工资统计)
    [YTU]_2769( 结构体--成绩统计)
    [YTU]_2577( 小数计算——结构体)
  • 原文地址:https://www.cnblogs.com/lushuicongsheng/p/1900421.html
Copyright © 2020-2023  润新知