XML简单读取问题(马上给分)

eileendl 2003-09-12 03:34:15
<?xml version="1.0" encoding="utf-8" ?>
<root>
<Property id="BorderStyle" type="Integer">
<Item id="None" value="0" />
<Item id="FixedSingle" value="1" />
<Item id="Fixed3D" value="2" />
</Property>
<Property id="DropDownStyle" type="Integer">
<Item id="Simple" value="0" />
<Item id="DropDown" value="1" />
<Item id="DropDownList" value="2" />
</Property>
</root>

我想从中根据Property的参数id 取出 它下面所有字节点item中的 id 和 value
...全文
72 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
eileendl 2003-09-12
  • 打赏
  • 举报
回复
ok,谢谢大家!
mainone 2003-09-12
  • 打赏
  • 举报
回复
读架构为
dataset.ReadXml(Application.StartupPath & "\a.xml", XmlReadMode.ReadSchema)
mainone 2003-09-12
  • 打赏
  • 举报
回复
dim t as integer =dataset.tables(1).rows.count
redim one(t):redim value(t)
For i = 0 To dataset.Tables(1).Rows.Count - 1
redim one(i):redim value(i)
row = dataset.Tables(1).Rows(i)
one(i) = row(0).ToString
value(i) = row(1).ToString
Next
mainone 2003-09-12
  • 打赏
  • 举报
回复
可是在之前的xml中你没有说明啊!!你可以REDIM 这个数组吗!??或者使用ARRAYLIST
如果你的xml文件很大的话可以先读他的架构,速度回快很多的
kexian 2003-09-12
  • 打赏
  • 举报
回复
Dim objDoc As New Xml.XmlDocument
Dim objNodeList As Xml.XmlNodeList
Dim objNode As Xml.XmlNode

objDoc.Load("Property.xml")
objNodeList = objDoc.GetElementsByTagName("root")
For Each objNode In objNodeList
If objNode.ParentNode.Name = "Document" Then
Me.Text = objNode.InnerText

拿去参考一下
eileendl 2003-09-12
  • 打赏
  • 举报
回复
to mainone
我觉得你的方法不是太好,我事先不知道有多少个property ,有多少个id阿,怎么定义one(4)??

to abiho(橡木)
好复杂阿,能不能有简单点儿的?

我现在的方法如下,我想改进一下,这样做好像效率不够高。
dim xDoc as xmlDocument
xDoc.Load("Property.xml")
For Each tmpnode In xDoc.Item("root").ChildNodes
If tmpnode.Attributes("id").Value = ListClass Then
For Each tmpnode1 In tmpnode.ChildNodes
取出:tmpnode1.Attributes("id").Value)
取出:tmpnode1.Attributes("value").Value
Next
Exit For
End If
Next

abiho 2003-09-12
  • 打赏
  • 举报
回复
The following example first creates a simple DataSet with one DataTable, two columns, and ten rows. The DataSet schema and data are written to disk by invoking the WriteXml method. A second DataSet is created and the ReadXml method is used to fill it with schema and data.

[Visual Basic, C#] Note This example shows how to use one of the overloaded versions of ReadXml. For other examples that might be available, see the individual overload topics.
[Visual Basic]
Private Sub DemonstrateReadWriteXMLDocumentWithXMLReader()
' Create a DataSet with one table and two columns.
Dim OriginalDataSet As New DataSet("myDataSet")
OriginalDataSet.Namespace = "NetFrameWork"
Dim myTable As New DataTable("myTable")
Dim c1 As New DataColumn("id", Type.GetType("System.Int32"))
c1.AutoIncrement = True
Dim c2 As New DataColumn("item")
myTable.Columns.Add(c1)
myTable.Columns.Add(c2)
OriginalDataSet.Tables.Add(myTable)
' Add ten rows.
Dim newRow As DataRow
Dim i As Integer
For i = 0 To 9
newRow = myTable.NewRow()
newRow("item") = "item " + i.ToString()
myTable.Rows.Add(newRow)
Next i
OriginalDataSet.AcceptChanges()
' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(OriginalDataSet, "Original DataSet")
' Write the XML schema and data to file with FileStream.
Dim xmlFilename As String = "myXmlDocument.xml"
' Create FileStream
Dim fsWriteXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Create)
' Create an XmlTextWriter to write the file.
Dim xmlWriter As New System.Xml.XmlTextWriter _
(fsWriteXml, System.Text.Encoding.Unicode)
' Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter)
' Close the FileStream.
fsWriteXml.Close()

' Dispose of the original DataSet.
OriginalDataSet.Dispose()
' Create a new DataSet.
Dim newDataSet As New DataSet("New DataSet")

' Read the XML document back in.
' Create new FileStream to read schema with.
Dim fsReadXml As New System.IO.FileStream _
(xmlFilename, System.IO.FileMode.Open)
' Create an XmlTextReader to read the file.
Dim myXmlReader As New System.Xml.XmlTextReader(fsReadXml)
' Read the XML document into the DataSet.
newDataSet.ReadXml(myXmlReader)
' Close the XmlTextReader
myXmlReader.Close()

' Print out values of each table in the DataSet using the
' function defined below.
PrintValues(newDataSet, "New DataSet")
End Sub


Private Sub PrintValues(ds As DataSet, label As String)
Console.WriteLine(ControlChars.Cr + label)
Dim t As DataTable
Dim r As DataRow
Dim c As DataColumn
For Each t In ds.Tables
Console.WriteLine("TableName: " + t.TableName)
For Each r In t.Rows
For Each c In t.Columns
Console.Write(ControlChars.Tab + " " + r(c).ToString())
Next c
Console.WriteLine()
Next r
Next t
End Sub
mainone 2003-09-12
  • 打赏
  • 举报
回复
Dim dataset As New DataSet()
dataset.ReadXml(Application.StartupPath & "\a.xml")
Dim i As Integer : Dim row As DataRow
Dim one(4) As String : Dim value(4) As String
For i = 0 To dataset.Tables(1).Rows.Count - 1
row = dataset.Tables(1).Rows(i)
one(i) = row(0).ToString
value(i) = row(1).ToString
Next

其中one 为id value 为对应值

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧