Search code examples
xmlvb.netxelement

Reading childelement to XElement in VB.NET


I've been trying to read an XML-file using VB.NET and after some (see 2-3 hours) googling and reading I've headed in somewhat the right direction, however now I'm at a stop and I can't find any answers that makes any sense to me.

This is the XML I have:

<?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>

In order to read this, I have the following code:

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

So, to my question. How can I access the children under the node <invoicingAddress> ??

the code I provided above didn't do much good. :P

Any help is dearly appreciated.

//Jaggen


Solution

  • Here is an ugly example of doing it, assuming doc contains the 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