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; }}
- C# 读取XML文档
- C#学习:读取XML
- C#读取XML文档
- C#读取XML配置文件
- C#读取XML文件
- C# 读取XML文档
- C#读取XML文件
- c#读取xml
- C#创建读取XML
- C#读取XML
- C#读取XML文件
- C# 读取XML
- c#读取xml文件
- c#读取XML
- C#读取XML文件
- C#读取XML文档
- C#读取XML文件
- c# 读取xml节点
- 图像去模糊算法在CUDA上的实现,基于MATLAB平台
- 长连接与短连接
- GoldenGate配置实例:RHEL 4.7下的Oracle 10g RAC到单实例的单向同步(二)
- 【大气红歌】著名歌唱家拉齐个人介绍
- JAVA调用系统命令或可执行程序
- C# 读取XML
- 在 Ajax 应用程序中实现实时数据推送
- 【Linux】ubuntu 创建桌面快捷方式,固定在启动器上
- IE6 a href onclick 执行顺序
- B树的实现(不带删除)
- svn cannot map the project with svn provider解决办法
- JAVA数据库连接池
- 这就是云,就这么简单!
- hibernate配置缩写jar包列表