John Janssen
John Janssen

Reputation: 323

Parsing XML using Linq in VB.NET

I am having an issue parsing an XML file and getting the results I am expecting. The scenario is I select an option out of a combobox. I have two options which I am returning from the XML file ("Cash Disbursements" and "Journal"). The problem I am having is the population of the next combobox. The values I am getting to populate the combobox with is with all of the "column name names".

Below is a list of my XML.

<file_import_types>
    <importtypes>
        <types import_type_name="Cash Disbursements">
            <type name="This - IS the name I want" import="Payments" uniquecol="2" countcol="5" ft="C">
                <columns>
                    <column name="test" Type="Double" Literal="1" GLNumber="0" Vend or="0" Format="" />
                    <column name="123" Type="String" Literal="1" GLNumber="0" Vendor="0" Format="String" />
                    <column name="Tester" Type="String" Literal="0" GLNumber="0" Vendor="0" Format="String" />
                    <column name="DATE" Type="DateTime" Literal="0" GLNumber="0" Vendor="0" Format="Date" />
                    <column name="test_again" Type="Double" Literal="1" GLNumber="1" Vendor="0" Format="" />
                    <column name="CountTest" Type="Double" Literal="0" GLNumber="0" Vendor="0" Format="" />
                    <column name="BlankTest" Type="DBNull" Literal="1" GLNumber="0" Vendor="0" Format="" />
                    <column name="DESCRIPTION" Type="String" Literal="0" GLNumber="0" Vendor="0" Format="String" />
                    <column name="NumberTest" Type="Double" Literal="0" GLNumber="1" Vendor="0" Format="" />
                    <column name="Thisisjustatest" Type="Double" Literal="0" GLNumber="0" Vendor="0" Format="Money" />
                </columns>
            </type>
        </types>
    </importtypes>
</file_import_types>

Here is my code:

'ACHFileType is the name of my second combobox that I am trying to populate.
ACHFileType.Items.Clear()
Dim importDefinitionElement As XElement = _
    (From xmlDefinitions As XElement In xmlFile.Descendants _
     Where xmlDefinitions.Attribute("import_type_name") = cbFilter.SelectedItem.ToString) _
    .FirstOrDefault
Dim query = From xmlDefinition As XElement In importDefinitionElement.Descendants Where xmlDefinition.Name = "type"
Dim formatType2 As New List(Of String)
Dim type As String = cbFilter.SelectedItem.ToString.Substring(0, 1)

For Each xmlColumn As XElement In query
    ' Loops through Columns in Excel
    If xmlColumn.Attribute("ft").Value.ToString = type Then
        For Each g As XAttribute In query.Elements.Elements.Attributes("name")
            Debug.Print(g.Value.ToString)
            formatType2.Add(g.Value)
        Next
    End If
Next

For Each a As String In formatType2
    ACHFileType.Items.Add(a.ToString())
    'The values that get populated here are "test", "123", "Tester", etc...  The value I am expecting is "This - Is the name I want"
Next       

Any help would be appreciated.

Upvotes: 0

Views: 1542

Answers (1)

Karl Anderson
Karl Anderson

Reputation: 34846

Since Dim query = From xmlDefinition As XElement In importDefinitionElement.Descendants Where xmlDefinition.Name = "type" is getting you the XElement for the type tag, then just do this:

Dim value = CType(query.Attribute("name"), String)

Upvotes: 1

Related Questions