zoukankan      html  css  js  c++  java
  • XML读取数据

          string strPath = System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Interface.xml");

            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); //定义XmlDocument对象

            xmlDoc.Load(strPath);

            //的是建立一个XPATH表达式,作用是查找所有名字是Item,属性ID=strWebCode的节点
            string xpath = "//Item[@WebCode='ssssqunar']";

              //获得xml文件的根节点
            XmlNode baseNode = xmlDoc.DocumentElement;
            //以根节点下的所有节点为查询返回,获得一个符合XPATH表达式的子节点
            XmlNode selectedNode = xmlDoc.SelectSingleNode(xpath);

            if (selectedNode != null)
            {
                string strUri = selectedNode.Attributes[2].InnerText;

                Response.Write(strUri);
            }
            else
            {
                Response.Write("未能请取数据");
            }

    我写的一个xml新闻系统的源代码希望对你有用

    一.添加数据:
    C# code

    public bool AddArticle(string NewsTitle, string NewsContent, string NewsClassID)
    {
    XmlDocument doc
    = new XmlDocument();
    doc.Load(HttpContext.Current.Server.MapPath(articlePath));
    //装载文章xml
    int newID = 1;
    if (doc.DocumentElement.SelectSingleNode("//Article[@ID]") != null)
    {
    //最后一个文章ID+1就是新的文章ID
    newID = Convert.ToInt32(doc.DocumentElement.SelectSingleNode("//Article[last()]").Attributes["ID"].Value) + 1;
    }
    XmlElement el
    = doc.CreateElement("Article");
    XmlAttribute id
    = doc.CreateAttribute("ID");
    id.Value
    = newID.ToString();
    XmlAttribute title
    = doc.CreateAttribute("Title");
    title.Value
    = NewsTitle;
    XmlAttribute date
    = doc.CreateAttribute("Date");
    date.Value
    = DateTime.Now.ToString();
    XmlAttribute classID
    = doc.CreateAttribute("ClassID");
    classID.Value
    = NewsClassID;
    XmlCDataSection content
    = doc.CreateCDataSection(NewsContent);

    el.Attributes.Append(id);
    el.Attributes.Append(title);
    el.Attributes.Append(classID);
    el.Attributes.Append(date);
    el.AppendChild(content);
    doc.DocumentElement.AppendChild(el);
    doc.Save(HttpContext.Current.Server.MapPath(articlePath));
    return true;
    }



    二.修改数据
    C# code

    public bool EditArticle(string NewsTitle, string NewsContent, string NewsID)
    {
    try
    {
    XmlDocument document
    = new XmlDocument();
    document.Load(HttpContext.Current.Server.MapPath(
    this.articlePath));
    XmlNode node
    = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
    if (node != null)
    {
    node.Attributes[
    "Title"].Value = NewsTitle;
    node.FirstChild.Value
    = NewsContent;
    }
    document.Save(HttpContext.Current.Server.MapPath(
    this.articlePath));
    return true;
    }
    catch
    {
    return false;
    }
    }



    三.删除数据
    C# code

    public bool DeleteArticle(string NewsID)
    {
    bool flag = false;
    try
    {
    XmlDocument document
    = new XmlDocument();
    document.Load(HttpContext.Current.Server.MapPath(
    this.articlePath));
    XmlNode oldChild
    = document.DocumentElement.SelectSingleNode("Article[@ID=" + NewsID + "]");
    if (oldChild != null)
    {
    oldChild.ParentNode.RemoveChild(oldChild);
    }
    document.Save(HttpContext.Current.Server.MapPath(
    this.articlePath));
    }
    catch
    {
    flag
    = false;
    }
    return flag;
    }



    转自:http://topic.csdn.net/u/20080612/23/f7d0b53e-27ca-4822-b0b9-46355db51fab.html
  • 相关阅读:
    Autofac
    MYSQL存储过程获取记录总数
    MYSQL通用存储过程
    判断用户用手机访问还是用电脑访问网页
    HttpRuntime.Cache与HttpContext.Current.Cache
    Eclipse之NDK编译-- Type 'jint' could not be resolved, and JNIEnv, jclass错误解决办法
    解决Android adjustresize全屏无效问题
    Android手机使用广播监听手机收到的短信
    使用Jackson解析首字母大写的json字符串
    Android Studio中设置提示函数用法
  • 原文地址:https://www.cnblogs.com/liangwei389/p/1289031.html
Copyright © 2011-2022 走看看