| 用C#读取XML的元素和属性 |
|
[ 作者:佚名 来源:网络 点击数:110 加入时间:2006-11-7 ] |
| 【双击左键自动滚屏】【图片上滚动鼠标滚轮变焦图片】
【字体:放大 正常 缩小】
字体颜色:
|
| 便民查询 中华五千年 世界五千年 万年历 天气预报 周公解梦 脑筋急转弯 在线翻译 电信话费查询 |
<?xml version="1.0" encoding="gb2312" ?>
<musiclession>
<student ID="s101">
<name>李华</name>
<age>12</age>
<score>92</score>
</student>
<student ID="s102">
<name>笑林</name>
<age>22</age>
<score>82</score>
</student>
<student ID="s103">
<name>王明</name>
<age>18</age>
<score>90</score>
</student>
</musiclession>
using System;
using System.Xml;
namespace DOMTest
{
class DOM
{
private static void PrintElement(XmlDocument document)
{
//获取所有的Node
XmlNodeList nodeList=document.GetElementsByTagName("*");
//打印每一个node的名称
for(int i=0;i<nodeList.Count;i++)
{
XmlNode node=nodeList.Item(i);
Console.WriteLine(node.Name);
}
}
private static void PrintAttributes(XmlDocument document)
{
XmlNodeList nodeList=document.GetElementsByTagName("*");
XmlNamedNodeMap nameNodeMap;
XmlElement element;
XmlAttribute attribute;
string attributeName;
string attributeValue;
for(int i=0;i<nodeList.Count;i++)
{
element=(XmlElement)nodeList.Item(i);
Console.WriteLine(element.Name+":"+element.ChildNodes[0].Value);
nameNodeMap=element.Attributes;
if(nameNodeMap!=null)
{
for(int j=0;j<nameNodeMap.Count;j++)
{
attribute=(XmlAttribute)nameNodeMap.Item(j);
attributeName=attribute.Name;
attributeValue=attribute.Value;
Console.WriteLine("属性是:"+attributeName+"="+attributeValue);
}
}
}
}
[STAThread]
static void Main(string[] args)
{
XmlDocument document =new XmlDocument();
document.Load("student.xml");
Console.WriteLine("元素是:");
PrintElement(document);
// Console.WriteLine("元素属性是:");
// PrintAttributes(document);
}
}
}
|
|
| 国家纸币 自考信息 度量转换 搜索引擎指南 城市经纬度 脑筋急转弯 各国资料 名言辞典 违章查询 |
·上一篇文章:Windows 消息大全使用详解 ·下一篇文章:怎样删除流氓软件-ewido |
| |
| 百度搜索更多内容:用C#读取XML的元素和属性 |
【推荐文档】 【打印文档】 【返回页首】 【关闭窗口】
|