I've recently just started working with XML and .NET.
I'm using XMLDocument to loop through an XML file. However, whilst I seem to be able to get the node value all I get for the node name is "#Text"
Below is the code I am using:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim doc As New XmlDocument
doc.Load(Server.MapPath("./XML/2010914.xml"))
Dim HTMLString As New StringBuilder("")
Dim Indent As String = " "
InfoLbl.Text = GetDataFromChildNodes(doc.DocumentElement.ChildNodes, HTMLString, Indent)
End Sub
Private Shared Function GetDataFromChildNodes(ByVal elements As XmlNodeList, ByVal builder As StringBuilder, ByVal Indent As String) As String
For Each node As XmlNode In elements
If node.HasChildNodes Then
GetDataFromChildNodes(node.ChildNodes, builder, Indent + Indent)
Else
builder.Append(Indent + node.Name & " ")
builder.Append(node.InnerText & "<br />")
End If
Next
Return builder.ToString()
End Function
Dim name as String
For Each node As XmlNode In elements
If node.HasChildNodes Then
GetDataFromChildNodes(node.ChildNodes, builder, Indent + Indent)
Else
name = String.Empty
If Not node.ParentNode Nothing Then
name = node.ParentNode.Name
End If
builder.Append(Indent + name & " ")
builder.Append(node.InnerText & "<br />")
End If
Next
YCS
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
Member
333 Points
374 Posts
Retrieving Node Name
Oct 05, 2011 05:22 AM|purplepint|LINK
Hi,
I've recently just started working with XML and .NET.
I'm using XMLDocument to loop through an XML file. However, whilst I seem to be able to get the node value all I get for the node name is "#Text"
Below is the code I am using:
Star
8111 Points
2100 Posts
Re: Retrieving Node Name
Oct 05, 2011 05:38 AM|chandrashekar|LINK
Hi,
Can you post your xml here.
Try the following:
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.
Member
333 Points
374 Posts
Re: Retrieving Node Name
Oct 05, 2011 05:48 AM|purplepint|LINK
Nice one Chandrashekar - I just needed to change node.name to node.parentnode.name
Star
8111 Points
2100 Posts
Re: Retrieving Node Name
Oct 05, 2011 06:19 AM|chandrashekar|LINK
Welcome. Hope you have tested and it gave you what you expected.
Please try the answer for the post and finally Don't forget to click “Mark as Answer” on the post that helped you.