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
原文地址:https://www.cnblogs.com/liangwei389/p/1289031.html