最近刚接触XML,买了本书看,发现不是很全面。没有提及怎么读取XML中元素的内容。求教怎么用C#读取XML中的数据,我想用来放进SQL数据库的。高手请指教。谢谢
解决方案 »
- 求截取字符串
- datagridview绑定datatable,datatable数据保存数据库
- 问下TcpClient的问题 怎么不断开连接持续的接收数据?
- 请问如何制作类似windwos 搜索向导的的程序100分
- 请问数据源的值变化后,控件的值会不会自动变化。
- WPF,为什么ScrollBar没有Orientation属性?
- 如何做积分有效期?
- 怎么制作C#程序的安装程序!
- 有那为朋友开发WinForm的信息管理系统的,请教大家如果通过结构来封装数据并对数据库的新增修改删除操作(讲的不一定到位).但就是那意思
- 火烧眉毛!怎样把用户控件作为里ListView的Item?!!!
- 读取文本文件问题
- 求帮忙。。。读文本问题
参考
<?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);
}
}
}
本文转摘自『蓝派网』http://www.lan27.com/Article/200705/946.htm
XmlDocument去MSDN上查一下这个类的方法就可以了 很简单的
ds.ReadXml(@"Your XML File Address");这个就可以,你试试!