i have some big size xml file (>500mb) like the one bellow.
<?xml version="1.0" encoding="UTF-8"?>
<ChangeRequests Count="" ID="" Update="">
<Requests>
<Request Type="Insert" Method="Customer">
<Country>xXx</Country>
<Window>10</Window>
<Door>11</Door>
<Id>88747</Id>
<Name>Lore Ipsum</Name>
<FirstName />
<OwnerFirstName />
<OwnerName />
<OwnerBirthday />
<CompanyName1 />
<CompanyName1 />
</Request>
<Request Type="Insert" Method="Customer">
<Country>xXx</Country>
<Window>10</Window>
<Door>11</Door>
<Id>444544</Id>
<Name>Lazy Dog</Name>
<FirstName />
<OwnerFirstName />
<OwnerName />
<OwnerBirthday />
<CompanyName1 />
<CompanyName1 />
</Request>
<Request Type="Insert" Method="Customer">
<Country>xXx</Country>
<Window>10</Window>
<Door>11</Door>
<Id>444544</Id>
<Name>Lazy Dog</Name>
<FirstName />
<OwnerFirstName />
<OwnerName />
<OwnerBirthday />
<CompanyName1 />
<CompanyName1 />
</Request>
</Requests>
</ChangeRequests>
i would like to read the first Request node and add to a combobox all of his childs without having to read other part of the xml. the code that i came up with is the one below but it goes through all of them and it will add to combo box alot of duplicate values.
Dim xml_doc As New XmlDocument
Dim xmlReader As New XmlTextReader(TextBox1.Text)
Dim arr As New ArrayList
xmlReader.MoveToContent()
xmlReader.ReadToFollowing("Request")
While xmlReader.Read()
Select Case xmlReader.NodeType
Case XmlNodeType.Element
ComboBox1.Items.Add("<" + xmlReader.Name & ">")
Exit Select
End Select
End While
答案 0 :(得分:0)
如果您只想阅读第一个Request
元素的子元素,可以检查Request
元素的结尾并退出循环:
While xmlReader.Read()
If xmlReader.NodeType = XmlNodeType.EndElement AndAlso xmlReader.Name = "Request" Then
Exit While
End If
Select Case xmlReader.NodeType
' ...
对于任何重复的子元素,这仍然会留下重复的内容,例如: <CompanyName1>
...但也许那没关系。如果你还想对它们进行重复数据删除,那么LINQ的Distinct
方法可以帮到那里:
Dim xmlReader As New XmlTextReader(TextBox1.Text)
xmlReader.MoveToContent()
xmlReader.ReadToFollowing("Request")
' Collect the names in a List
Dim elementNames As New List(Of String)()
While xmlReader.Read()
Select Case xmlReader.NodeType
Case XmlNodeType.Element
elementNames.Add("<" + xmlReader.Name & ">")
Exit Select
End Select
End While
' Add distinct values to the ComboBox
ComboBox1.Items.AddRange(elementNames.Distinct().ToArray())