Asp.net 从xml文件中选择多个元素,并显示在多个标签中
我需要从xml文件中选择多个元素的帮助。我目前只知道如何选择一个,试图找出不止一个是伤害我的小kopri。谢谢你抽出时间Asp.net 从xml文件中选择多个元素,并显示在多个标签中,asp.net,Asp.net,我需要从xml文件中选择多个元素的帮助。我目前只知道如何选择一个,试图找出不止一个是伤害我的小kopri。谢谢你抽出时间 Imports System.Xml Partial Class ex01_Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'ins
Imports System.Xml
Partial Class ex01_Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'instantiate a reader'
Dim reader As New XmlTextReader(Server.MapPath("~/ex01/docP.xml"))
'declare variable to record when a <name> element is found'
Dim bName As Boolean = False
'iterate through all of the nodes in the XML document'
While reader.Read()
'look at the node type of each node'
Select Case reader.NodeType
'if node is a <name> element, remember it'
Case XmlNodeType.Element
If reader.Name = "title" Then
bName = True
End If
'if node is text & previous node was <name>, add it to Label'
Case XmlNodeType.Text
If bName Then
lblDisplayXml.Text &= reader.ReadString & "<br/>"
bName = False
End If
End Select
End While
End Sub
End Class
Imports System.Xml
部分类ex01\U默认值
继承System.Web.UI.Page
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
“实例化一个读者”
将读卡器设置为新的XmlTextReader(Server.MapPath(“~/ex01/docP.xml”))
'在找到元素时声明要记录的变量'
Dim bName作为布尔值=False
'遍历XML文档中的所有节点'
而reader.Read()
'查看每个节点的节点类型'
选择Case reader.NodeType
'如果节点是元素,请记住它'
Case XmlNodeType.Element
如果reader.Name=“title”,则
bName=True
如果结束
'如果节点为文本&上一个节点为,则将其添加到标签'
Case XmlNodeType.Text
如果是bName那么
lblDisplayXml.Text&=reader.ReadString&“
”
bName=False
如果结束
结束选择
结束时
端接头
末级
xml文件:
<book_club>
<book>
<isbn>0-13-129014-2</isbn>
<title>JAVA How to Program (6th Ed)</title>
<author>PJ & HM Deitel</author>
<price>£39.99</price>
</book>
<book>
<isbn>0-67-232238-2</isbn>
<title>Teach Yourself UML</title>
<author>J Schmuller</author>
<price>£9.99</price>
</book>
<book>
<isbn>0-27-365575-2</isbn>
<title>Practical Business Systems Development using SSADM</title>
<author>P Weaver, N Lambrou & M Walkley</author>
<price>£34.99</price>
</book>
<book>
<isbn>0-67-232422-9</isbn>
<title>XML Primer Plus</title>
<author>N Chase</author>
<price>£32.99</price>
</book>
<book>
<isbn>0-78-972476-6</isbn>
<title>XML and Java from Scratch</title>
<author>N Chase</author>
<price>£19.99</price>
</book>
<book>
<isbn>1234567890</isbn>
<title>ASP.NET for Dummies</title>
<author>RUA Dummy</author>
<price>free!!</price>
</book>
</book_club>
0-13-129014-2
JAVA如何编程(第六版)
PJ&;HM Deitel
£39.99
0-67-232238-2
自学UML
施穆勒
£9.99
0-27-365575-2
使用SSADM开发实用业务系统
P Weaver、N Lambrou和;M沃克利
£34.99
0-67-232422-9
XML初级读物升级版
N蔡斯
£32.99
0-78-972476-6
XML和Java从头开始
N蔡斯
£19.99
1234567890
ASP.NET虚拟机
RUA假人
自由的
我对VB不太了解,因为我使用C#。我使用LinQ,因为它使用XDocument类提供了一种新的读取XML的方法。这是我做的,我从字符串中读取:
返回IEnumerable()的方法
为book创建一个类 公共课堂用书 { 公共图书(){}
}r您可能会使用.net>=3.5,如果是,请尝试linq
XDocument doc = XDocument.Parse(myString);
var query= from x in doc.Descendants("Book")
select new Location
{
ISBN= Double.Parse(
x.Descendants("isbn").First().Value),
Author= Double.Parse(
x.Descendants("author").First().Value) ......
};
return query;
public string Author {
get; set;
}
.....