0

我在解析 XML 文件并获得预期结果时遇到问题。场景是我从组合框中选择一个选项。我有两个从 XML 文件返回的选项(“现金支付”和“日志”)。我遇到的问题是下一个组合框的人口。我用来填充组合框的值是所有“列名”。

下面是我的 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>

这是我的代码:

'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       

任何帮助,将不胜感激。

4

1 回答 1

1

既然Dim query = From xmlDefinition As XElement In importDefinitionElement.Descendants Where xmlDefinition.Name = "type"是给你XElementtype标签,那么就这样做:

Dim value = CType(query.Attribute("name"), String)
于 2013-08-23T19:38:35.027 回答