C# 读取XML

来源:互联网 发布:饥荒修改数据手杖 编辑:程序博客网 时间:2024/06/05 18:13

1:本文主要讲的C#读取 XML。如有错误之处还请大家指出.谢谢。 

2:这是整个项目的截图

3:xml文件

<?xml version="1.0" encoding="utf-8" ?><Students class="1" grade="2">  <student>    <name>张三</name>    <age>16</age>    <adress>陕西,西安</adress>    <gender>男</gender>  </student>  <student>    <name>李四</name>    <age>21</age>    <adress>陕西,宝鸡</adress>    <gender>男</gender>  </student>  <student>    <name>王五</name>    <age>19</age>    <adress>陕西,咸阳</adress>    <gender>男</gender>  </student>  <student>    <name>小翠</name>    <age>18</age>    <adress>陕西,渭南</adress>    <gender>女</gender>  </student>  <student>    <name>小花</name>    <age>17</age>    <adress>陕西,商洛</adress>    <gender>女</gender>  </student></Students>

4:在web.config  的configSections节点下添加

<DATA>    <add key="System.Configuration.File" value="DataConfiguration\Data.xml"/>  </DATA>

5:Default.aspx 页面

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>Untitled Page</title></head><body>    <form id="form1" runat="server">    <div>     </div>    </form></body></html>

 

6:Default.aspx.cs

using System;using System.Collections.Specialized;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Collections;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.IO;using System.Xml;public partial class _Default : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!IsPostBack)        {            ReadXmlFile();        }    }    private string GetXmlDocumentPath()    {        string path = string.Empty;        path = Project.Instance.GetAppConfigFile();        return path;       }    private void ReadXmlFile()    {        string rootName="Students";        XmlDocument xmlDocument = LoadXmlFile();        XmlNode xmlNode = GetXmlNodeRoot(xmlDocument, rootName);        XmlNodeList xmlNodeList = GetXmlNodeListParent(xmlNode);        XmlNodeList xml = null;        for (int i = 0; i < xmlNodeList.Count; i++)        {            XmlNode xn = xmlNodeList[i];            XmlElement xmlElement = (XmlElement)xn;            xml = xmlElement.ChildNodes;            for (int y = 0; y < xml.Count; y++)            {                Response.Write(xml[y].Name + ":" + xml[y].InnerText + "<br/>");            }            Response.Write("++++++++++++++++++++++++++++++++++++++++++++++++<br/>");        }    }    private XmlNode GetXmlNodeRoot(XmlDocument xmlDcoument,string xmlRootName)    {        XmlNode xmlNode = xmlDcoument.SelectSingleNode(xmlRootName);        return xmlNode;    }    private XmlNodeList GetXmlNodeListParent(XmlNode xmlNode)    {        XmlNodeList xmlNodeList = xmlNode.ChildNodes;        return xmlNodeList;    }    private XmlDocument LoadXmlFile()    {        XmlDocument xmlDoucment = new XmlDocument();        xmlDoucment.Load(GetXmlDocumentPath());        return xmlDoucment;    }}


 

7:Project 类

using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Collections.Specialized;using System.IO;/// <summary>/// Summary description for Project/// </summary>public class Project{public Project(){//// TODO: Add constructor logic here//}    private static Project instance;    public static Project Instance    {        get        {            if (instance == null)            {                lock (typeof(Project))                {                    if (instance == null)                    {                        instance = new Project();                    }                }            }            return instance;        }    }    public string  Initialize(string applicationPath)    {        NameValueCollection valueCollection =            ConfigurationManager.GetSection("DATA") as NameValueCollection;        if (valueCollection == null)        {            throw new ApplicationException(                "DATA section is missing in Web.config.");        }        string configFile = valueCollection["System.Configuration.File"];        configFile = NormalizePath(configFile, applicationPath);        return configFile;    }    public string  GetAppConfigFile()    {        string result = string.Empty;        string path = AppDomain.CurrentDomain.BaseDirectory;        result=Initialize(path);        return result;    }    private string NormalizePath(string localPath, string applicationPath)    {        string path = string.Empty;        if (localPath.StartsWith("/") ||            localPath.StartsWith("~") ||            applicationPath.Equals("/"))        {            path = localPath;        }        else        {            path = Path.Combine(applicationPath, localPath);        }        return path;    }}


 

原创粉丝点击