在VB.NET中读取childelement到XElement

时间:2011-12-22 14:09:16

标签: xml vb.net xelement

我一直在尝试使用VB.NET读取XML文件,经过一段时间(见2-3小时)谷歌搜索和阅读后,我走向了一个正确的方向,但现在我停下来了我找不到任何对我有意义的答案。

这是我的XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
<exportCustomerInformation xmlns="http://www.testdom.se/server/services/exportCustomerInformationNavision" xmlns:met="http://www.testdom.se/server/services/metadata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <data>
        <customers>
            <customerInformation xmlns:nav="http://www.testdom.se/server/services/integration/navision">
                <nav:littera/>
                <nav:parentUuid/>
                <nav:name>TESTBEDRIFTEN AS</nav:name>
                <nav:uuid>77cf992e-766a-4496-9a5a-5105a75214ce</nav:uuid>
                <nav:customerType>1</nav:customerType>
                <nav:emailAddress/>
                <nav:industryBranch/>
                <nav:department>TRONDHEIM</nav:department>
                <nav:invoicingAddress>
                    <nav:streetName>PO BOX 123</nav:streetName>
                    <nav:streetNumber/>
                    <nav:recipient/>
                    <nav:postalCode>7407</nav:postalCode>
                    <nav:city>TRONDHEIM</nav:city>
                    <nav:country>NORWAY</nav:country>
                </nav:invoicingAddress>
                <nav:deliveryAddress>
                    <nav:placeName>TESTBEDRIFTEN AS</nav:placeName>
                <nav:streetName>STORGATEN</nav:streetName>
                    <nav:streetNumber>1</nav:streetNumber>
                    <nav:recipient/>
                    <nav:postalCode>7011</nav:postalCode>
                    <nav:city>TRONDHEIM</nav:city>
                    <nav:country>NORWAY</nav:country>
                </nav:deliveryAddress>
            </customerInformation>
        </customers>
    </data>
</exportCustomerInformation>

为了阅读本文,我有以下代码:

Dim xmlDoc = XDocument.Load(file)
    For Each dataXML As XElement In xmlDoc...<customerInformation>

        oDR = oDT.NewRow
        oDR("parentUuid") = dataXML...<nav:parentUuid>.Value
        oDR("name") = dataXML...<nav:name>.Value
        oDR("uuid") = dataXML...<nav:uuid>.Value
        oDR("customerType") = dataXML...<nav:customerType>.Value
        oDR("emailAddress") = dataXML...<nav:emailAddress>.Value

        'What to here in order to get to these children?
        oDR("invStreetName") = dataXML.<nav:invoicingAddress>.<nav:invStreetName>.Value
        oDR("invStreetNumber") = dataXML.<nav:invoicingAddress>...<nav:invStreetNumber>.Value
        oDR("invName") = dataXML.<nav:invoicingAddress>...<nav:invName>.Value
        oDR("invPostalCode") = dataXML.<nav:invoicingAddress>...<nav:invPostalCode>.Value
        oDR("invCity") = dataXML.<nav:invoicingAddress>...<nav:invCity>.Value
        oDR("invCountry") = dataXML.<nav:invoicingAddress>...<nav:invCountry>.Value

    Next

所以,对我的问题。如何在节点&lt; invoicingAddress&gt;下访问子节点??

我上面提供的代码并没有太大的帮助。 :P

非常感谢任何帮助。

// Jaggen

1 个答案:

答案 0 :(得分:0)

这是一个丑陋的例子,假设doc包含XDocument:

Dim customerInfo = doc.Root.Elements.First.Elements.First.Elements.First

Dim invoicingAddress = _
customerInfo.Elements.First(Function(x) x.Name.LocalName = "invoicingAddress")

Dim streetName = _
invoicingAddress.Elements.First(Function(x) x.Name.LocalName = "streetName").Value