Answered by:
open and read xml file with multiple nodes

Question
-
User1717218719 posted
Hi all,
I am looking to read through an xml file and display the text in an excel file. so far I can read through a basic xml file but I would like to read through a more complex one with multiple values and nodes. If anyone cpould help that would be great.
here is the code I have so far:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim FileUpload1 As FileUpload = TryCast(FindControl("FileUpload"), FileUpload) Try If FileUpload.HasFile And FileUpload.FileContent IsNot Nothing Then Dim Path As String = Server.MapPath("~/Files/") + Path.GetFileName(FileUpload.PostedFile.FileName) Dim xmlFile As XmlReader xmlFile = XmlReader.Create(Path, New XmlReaderSettings()) Dim ds As New DataSet ds.ReadXml(xmlFile) For i = 0 To ds.Tables(0).Rows.Count - 1 MsgBox(ds.Tables(0).Rows(i).Item(4)) Next End If Catch ex As Exception MsgBox(ex.Message) End Try End Sub
<Table> <Header> <Date>2019-04-17</Date> <ID>1852b</ID> <Category>A</Category> </Header> </Table>
The above works but not for the below xml code when it gets to the <address> the messagebox reads '0' not the xml tag text.
<Table> <Header> <Date>2019-04-17</Date> <ID>1852b</ID> <Category>A</Category> <Address> <AddressLine1>18</AddressLine1> <AddressLine2>Yellow brickroad</AddressLine2> <AddressLine3> </AddressLine3> <CityName>London</CityName> <CountryName>UK</CountryName> </Address> </Header> </Table>
Thursday, October 29, 2020 2:51 PM
Answers
-
User1686398519 posted
Hi E.RU,
According to your needs, you can modify your code like this. Here are two solutions for you to refer to.
Dim xmlFile As XmlReader = XmlReader.Create(Path, New XmlReaderSettings()) Dim ds As DataSet = New DataSet() While xmlFile.Read() Select Case xmlFile.NodeType Case XmlNodeType.Element label1.Text += "<" & xmlFile.Name & ">" Case XmlNodeType.Text label1.Text += xmlFile.Value Case XmlNodeType.EndElement label1.Text += "</br>" End Select End While
Or
Dim xmlFile As XmlReader = XmlReader.Create(Path, New XmlReaderSettings()) Dim ds As DataSet = New DataSet() ds.ReadXml(xmlFile) For Each table As DataTable In ds.Tables For Each row As DataRow In table.Rows For Each column As DataColumn In table.Columns label1.Text += "</br>" & row(column) Next Next Next
Here is the result.
Best Regards,
YihuiSun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 2, 2020 3:18 AM
All replies
-
User475983607 posted
.NET comes with very robust XML APIs. Read the docs. Learn XML fundamentals and find an API that fits your needs.
https://docs.microsoft.com/en-us/dotnet/standard/data/xml/
E.RU
but I would like to read through a more complex one with multiple values and nodes. If anyone cpould help that would be great.There no magic here. You need to know the XML schema to read values. Please take a few moments to run your code through the debugger and update your code to handle new schema.
Thursday, October 29, 2020 3:02 PM -
User1686398519 posted
Hi E.RU,
According to your needs, you can modify your code like this. Here are two solutions for you to refer to.
Dim xmlFile As XmlReader = XmlReader.Create(Path, New XmlReaderSettings()) Dim ds As DataSet = New DataSet() While xmlFile.Read() Select Case xmlFile.NodeType Case XmlNodeType.Element label1.Text += "<" & xmlFile.Name & ">" Case XmlNodeType.Text label1.Text += xmlFile.Value Case XmlNodeType.EndElement label1.Text += "</br>" End Select End While
Or
Dim xmlFile As XmlReader = XmlReader.Create(Path, New XmlReaderSettings()) Dim ds As DataSet = New DataSet() ds.ReadXml(xmlFile) For Each table As DataTable In ds.Tables For Each row As DataRow In table.Rows For Each column As DataColumn In table.Columns label1.Text += "</br>" & row(column) Next Next Next
Here is the result.
Best Regards,
YihuiSun
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, November 2, 2020 3:18 AM -
User1717218719 posted
This is exactly what I was looking for thanks
Monday, November 2, 2020 11:58 AM