c# 读取xml文件
<?xmlversion="1.0"?>-<!--Thisfilerepresentsafragmentofabookstoreinventorydatabase-->-...
<?xml version="1.0" ?>
- <!-- This file represents a fragment of a book store inventory database
-->
- <bookstore>
- <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
- <author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
- <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
- <author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
- <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
- <author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
我想读出genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0 这三个值存贮到数据库中,要怎么做? 展开
- <!-- This file represents a fragment of a book store inventory database
-->
- <bookstore>
- <book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
- <author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
- <book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">
<title>The Confidence Man</title>
- <author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
- <book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">
<title>The Gorgias</title>
- <author>
<first-name>Sidas</first-name>
<last-name>Plato</last-name>
</author>
<price>9.99</price>
</book>
</bookstore>
我想读出genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0 这三个值存贮到数据库中,要怎么做? 展开
展开全部
//加载XML文件
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\Documents and Settings\Administrator\桌面\test\ConsoleApplication1\ConsoleApplication1\XMLTest.xml");
//选择所有的book节点
XmlNodeList xnl = xml.SelectNodes("//book");
//循环获取每个book节点的 genre publicationdate ISBN 属性
foreach (XmlNode xn in xnl)
{
string genre = xn.Attributes["genre"].Value;
string publicationdate = xn.Attributes["publicationdate"].Value;
string ISBN = xn.Attributes["ISBN"].Value;
Console.WriteLine("genre:" + genre + " publicationdate:" + publicationdate + " ISBN:" + ISBN);
}
Console.ReadLine();
XmlDocument xml = new XmlDocument();
xml.Load(@"C:\Documents and Settings\Administrator\桌面\test\ConsoleApplication1\ConsoleApplication1\XMLTest.xml");
//选择所有的book节点
XmlNodeList xnl = xml.SelectNodes("//book");
//循环获取每个book节点的 genre publicationdate ISBN 属性
foreach (XmlNode xn in xnl)
{
string genre = xn.Attributes["genre"].Value;
string publicationdate = xn.Attributes["publicationdate"].Value;
string ISBN = xn.Attributes["ISBN"].Value;
Console.WriteLine("genre:" + genre + " publicationdate:" + publicationdate + " ISBN:" + ISBN);
}
Console.ReadLine();
展开全部
把文本保存成xml文件放在程序的运行目录下面
using System.Xml;
static void Main(string[] args)
{
string genre = string.Empty;
string publicDate = string.Empty;
string isbn = string.Empty;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("123.xml"); //doc.LoadXml("xml字符串")
XmlNodeList bookStores = doc.GetElementsByTagName("bookstore");
if(bookStores.Count > 0)
{
XmlNode bookStore = bookStores[0];
foreach(XmlNode bookNode in bookStore.ChildNodes)
{
if(bookNode.Attributes["genre"].Value == "autobiography")
{
//查找到节点
genre = bookNode.Attributes["genre"].Value;
publicDate = bookNode.Attributes["publicationdate"].Value;
isbn = bookNode.Attributes["ISBN"].Value;
}
}
}
Console.WriteLine(genre);
Console.WriteLine(publicDate);
Console.WriteLine(isbn);
Console.Read();
}
using System.Xml;
static void Main(string[] args)
{
string genre = string.Empty;
string publicDate = string.Empty;
string isbn = string.Empty;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load("123.xml"); //doc.LoadXml("xml字符串")
XmlNodeList bookStores = doc.GetElementsByTagName("bookstore");
if(bookStores.Count > 0)
{
XmlNode bookStore = bookStores[0];
foreach(XmlNode bookNode in bookStore.ChildNodes)
{
if(bookNode.Attributes["genre"].Value == "autobiography")
{
//查找到节点
genre = bookNode.Attributes["genre"].Value;
publicDate = bookNode.Attributes["publicationdate"].Value;
isbn = bookNode.Attributes["ISBN"].Value;
}
}
}
Console.WriteLine(genre);
Console.WriteLine(publicDate);
Console.WriteLine(isbn);
Console.Read();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
XmlDocument xmlDoc = new XmlDocument();
fpath = Server.MapPath("../swf/network.xml");//xml路径
xmlDoc.Load(fpath);
XmlNode root = xmlDoc.SelectSingleNode("bookstore");
for (int k = 0; k < root.ChildNodes.Count; k++)
{
string genre=root.ChildNodes[0].Attributes[0].Value;
string publicationdate=root.ChildNodes[0].Attributes[1].Value;
string ISBN=root.ChildNodes[0].Attributes[2].Value;
}
当然你还可以循环取到任何你想要的值
fpath = Server.MapPath("../swf/network.xml");//xml路径
xmlDoc.Load(fpath);
XmlNode root = xmlDoc.SelectSingleNode("bookstore");
for (int k = 0; k < root.ChildNodes.Count; k++)
{
string genre=root.ChildNodes[0].Attributes[0].Value;
string publicationdate=root.ChildNodes[0].Attributes[1].Value;
string ISBN=root.ChildNodes[0].Attributes[2].Value;
}
当然你还可以循环取到任何你想要的值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先将bookstore标签找到,在找下面的book标签,在讲book的属性个数获取,进行循环,根据索引获取想对应的值就行了!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询