Get XML attributes

ahbenshaut

Well-known member
Joined
Oct 29, 2004
Messages
62
Location
Alaska
Programming Experience
5-10
Good Day to all.
I am having an interesting time parsing an XML file. Any help would be greatly appreciated. My biggest hurdle is getting node attributes. Here is what I have as an XML example:
<?xml version="1.0" ?>
<ILS_BULK_EXPORT>
<Loan MISMOVersionIdentifier="2.6" E3_ApplicationNumber="1234567890" E3_LoanNumber="0987654321" E3_MapLastUpdated=" 01/01/1900">
<_APPLICATION _ID="somelongnumber">
<_DATA_INFORMATION>
<ADDITIONAL_CASE_DATA>
<TRANSMITTAL_DATA CurrentSomethingOrAnother="blah"
and on and on...
</ILS_BULK_EXPORT>

What I am trying to do is get specific node attributes, E3_ApplicationNumber and E3_LoanNumber, for starters
and my code:
Private Sub ParseFile()
Try
Dim m_xmlDoc As XmlDocument
Dim m_nodeList As XmlNodeList
Dim m_node As XmlNode
m_xmlDoc = New XmlDocument
'm_xmlDoc.Load(txtFileToParse.Text)
m_xmlDoc.Load("C:\Documents and Settings\jmuslin\Desktop\AfterUpgrade.xml")
m_nodeList = m_xmlDoc.DocumentElement.SelectNodes("Loan MISMOVersionIdentifier='2.6' ")
For Each m_node In m_nodeList
MessageBox.Show(m_node.Attributes("E3_ApplicationNumber").Value.ToString)
Next
'm_nodeList = m_xmlDoc.SelectNodes("ILS_BULK_EXPORT/Loan MISMOVersionIdentifier='2.6' ")
'For Each m_node In m_nodeList
' Dim appNumber = m_node.Attributes.GetNamedItem("E3_LoanNumber").Value
' MessageBox.Show(appNumber)
'Next
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

When I debug the code, I get the following error: 'Loan MISMOVersionIdentifier='2.6' ' has an invalid token. I have no idea what that means...

thank you in advance for the help. May the schwartz be with you! :)
 

Lotok

Well-known member
Joined
Jan 17, 2012
Messages
198
Location
Scotland
Programming Experience
5-10
Firstly your XML is not formatted correctly. I changed what you posted above to this, you need to close tags.

VB.NET:
<?xml version="1.0" ?>
<ILS_BULK_EXPORT>
<Loan MISMOVersionIdentifier="2.6" E3_ApplicationNumber="1234567890" E3_LoanNumber="0987654321" E3_MapLastUpdated=" 01/01/1900" />
<_APPLICATION _ID="somelongnumber" />
<_DATA_INFORMATION />
<ADDITIONAL_CASE_DATA />
<TRANSMITTAL_DATA CurrentSomethingOrAnother="blah">
and on and on...</TRANSMITTAL_DATA>
</ILS_BULK_EXPORT>

For querying your XML I would use LINQ. Here is an example of pulling the Application Number in a console application.

Imports System.Linq
Module Module1

    Sub Main()
        Dim document As XDocument = XDocument.Load("c:\temp\test.xml")
        Dim applicationNumber = From i In document.Descendants() Where i.Attribute("E3_ApplicationNumber") IsNot Nothing
                   Select i.Attribute("E3_ApplicationNumber").Value

        Console.WriteLine(applicationNumber.First())
        Console.ReadLine()

    End Sub

End Module
 
Last edited:
Top Bottom