0

I have an XML file for VB.NET to read. My code is specific to the file.

How can I read the value of the <Value> associated with the <Key> LNGYIL?

enter image description here

My code:

Dim doc As XDocument
        doc = XDocument.Load("c:\jpan\faturalar.xml")
        Dim lngyil= (
       From v In doc.<soap>.<soap>.<IntegrationGetEntitySetWithPacketLoginResponse>.<IntegrationGetEntitySetWithPacketLoginResult>.<ResultEntitySet>.<CustomData>.<ArrayOfClsDictionaryOfStringObject>.<clsDictionaryOfStringObject>
       Where v.<Key>.Value = "LNGYIL"
       Select v.<Value>.Value
            ).Single()
        ListBox1.Items.Insert(0, lngyil)

XML source:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><IntegrationGetEntitySetWithPacketLoginResponse xmlns="http://integration.univera.com.tr"><IntegrationGetEntitySetWithPacketLoginResult><ResultString>OK</ResultString><IsSuccess>true</IsSuccess><ResultEntitySet><GorevSonucBilgiler/><DistUnvan/><PaketAdi/><Warnings/><PaketTanimlar><clsPaketTanim><Kod xsi:nil="true"/><Tabloadi>CUSTOMVIEW_ENTVIEWFATURAGENERIC_OZGUN</Tabloadi><Viewadi>CUSTOMVIEW_ENTVIEWFATURAGENERIC_OZGUN</Viewadi><Yon xsi:nil="true"/><Kriter>"(LNGDISTKOD = 12) AND BYTTUR IN(0,1) AND (LNGYIL = 2018) AND (BYTDURUM = 0)"</Kriter><Durum xsi:nil="true"/></clsPaketTanim></PaketTanimlar><SatirBazliTransaction>false</SatirBazliTransaction><LogKategori>0</LogKategori><IntegrationGorevSonucTip>0</IntegrationGorevSonucTip><errorLoglist/><SCCall>false</SCCall><ReturnLoglist>false</ReturnLoglist><StokSil>false</StokSil><CustomData><ArrayOfClsDictionaryOfStringObject><clsDictionaryOfStringObject><Key>LNGYIL</Key><Value xsi:type="xsd:int">2018</Value></clsDictionaryOfStringObject><clsDictionaryOfStringObject><Key>LNGDISTKOD</Key><Value xsi:type="xsd:int">12</Value></clsDictionaryOfStringObject><clsDictionaryOfStringObject><Key>TXTDISTKOD</Key><Value xsi:type="xsd:string">SH012</Value></clsDictionaryOfStringObject><clsDictionaryOfStringObject><Key>TXTSTKOD</Key><Value xsi:type="xsd:string">SH010</Value></clsDictionaryOfStringObject></ArrayOfClsDictionaryOfStringObject></CustomData></ResultEntitySet></IntegrationGetEntitySetWithPacketLoginResult></IntegrationGetEntitySetWithPacketLoginResponse></soap:Body></soap:Envelope>

Currently I get nothing.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84

1 Answers1

0

You need to code it to deal with the namespaces. One way is to use Imports statements like this:

Imports <xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
Imports <xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Imports <xmlns:xsd="http://www.w3.org/2001/XMLSchema">
Imports <xmlns="http://integration.univera.com.tr">

Module Module1

    Sub Main()
        Dim x As XElement = <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                                <soap:Body>
                                    <IntegrationGetEntitySetWithPacketLoginResponse xmlns="http://integration.univera.com.tr">
                                        <IntegrationGetEntitySetWithPacketLoginResult>
                                            <ResultString>OK</ResultString>
                                            <IsSuccess>true</IsSuccess>
                                            <ResultEntitySet>
                                                <GorevSonucBilgiler/>
                                                <DistUnvan/>
                                                <PaketAdi/>
                                                <Warnings/>
                                                <PaketTanimlar>
                                                    <clsPaketTanim>
                                                        <Kod xsi:nil="true"/>
                                                        <Tabloadi>CUSTOMVIEW_ENTVIEWFATURAGENERIC_OZGUN</Tabloadi>
                                                        <Viewadi>CUSTOMVIEW_ENTVIEWFATURAGENERIC_OZGUN</Viewadi>
                                                        <Yon xsi:nil="true"/>
                                                        <Kriter>"(LNGDISTKOD = 12) AND BYTTUR IN(0,1) AND (LNGYIL = 2018) AND (BYTDURUM = 0)"</Kriter>
                                                        <Durum xsi:nil="true"/>
                                                    </clsPaketTanim>
                                                </PaketTanimlar>
                                                <SatirBazliTransaction>false</SatirBazliTransaction>
                                                <LogKategori>0</LogKategori>
                                                <IntegrationGorevSonucTip>0</IntegrationGorevSonucTip>
                                                <errorLoglist/>
                                                <SCCall>false</SCCall>
                                                <ReturnLoglist>false</ReturnLoglist>
                                                <StokSil>false</StokSil>
                                                <CustomData>
                                                    <ArrayOfClsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>LNGYIL</Key>
                                                            <Value xsi:type="xsd:int">2018</Value>
                                                        </clsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>LNGDISTKOD</Key>
                                                            <Value xsi:type="xsd:int">12</Value>
                                                        </clsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>TXTDISTKOD</Key>
                                                            <Value xsi:type="xsd:string">SH012</Value>
                                                        </clsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>TXTSTKOD</Key>
                                                            <Value xsi:type="xsd:string">SH010</Value>
                                                        </clsDictionaryOfStringObject>
                                                    </ArrayOfClsDictionaryOfStringObject>
                                                </CustomData>
                                            </ResultEntitySet>
                                        </IntegrationGetEntitySetWithPacketLoginResult>
                                    </IntegrationGetEntitySetWithPacketLoginResponse>
                                </soap:Body>
                            </soap:Envelope>

        Dim doc = New XDocument(x)

        Dim q = doc.<soap:Envelope>.<soap:Body>.<IntegrationGetEntitySetWithPacketLoginResponse>.<IntegrationGetEntitySetWithPacketLoginResult>.<ResultEntitySet>.<CustomData>.<ArrayOfClsDictionaryOfStringObject>.<clsDictionaryOfStringObject>

        Dim lngyil = (
                       From v In q
                       Where v.<Key>.Value = "LNGYIL"
                       Select v.<Value>.Value
                     ).Single()

        Console.WriteLine(lngyil)

        Console.ReadLine()

    End Sub

End Module

Outputs:

2018

P.S. I have a suspicion that xmlns="http://integration.univera.com.tr" is not valid, and it should be something like xmlns:univera="http://integration.univera.com.tr". That change makes the following possible...

A second option

Instead of using Imports statements, you can use an XmlNamespaceManager like this:

Imports System.Xml
Imports System.Xml.XPath

Module Module1

    Sub Main()
        Dim x As XElement = <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                                <soap:Body>
                                    <IntegrationGetEntitySetWithPacketLoginResponse xmlns:univera="http://integration.univera.com.tr">
                                        <IntegrationGetEntitySetWithPacketLoginResult>
                                            <ResultString>OK</ResultString>
                                            <IsSuccess>true</IsSuccess>
                                            <ResultEntitySet>
                                                <GorevSonucBilgiler/>
                                                <DistUnvan/>
                                                <PaketAdi/>
                                                <Warnings/>
                                                <PaketTanimlar>
                                                    <clsPaketTanim>
                                                        <Kod xsi:nil="true"/>
                                                        <Tabloadi>CUSTOMVIEW_ENTVIEWFATURAGENERIC_OZGUN</Tabloadi>
                                                        <Viewadi>CUSTOMVIEW_ENTVIEWFATURAGENERIC_OZGUN</Viewadi>
                                                        <Yon xsi:nil="true"/>
                                                        <Kriter>"(LNGDISTKOD = 12) AND BYTTUR IN(0,1) AND (LNGYIL = 2018) AND (BYTDURUM = 0)"</Kriter>
                                                        <Durum xsi:nil="true"/>
                                                    </clsPaketTanim>
                                                </PaketTanimlar>
                                                <SatirBazliTransaction>false</SatirBazliTransaction>
                                                <LogKategori>0</LogKategori>
                                                <IntegrationGorevSonucTip>0</IntegrationGorevSonucTip>
                                                <errorLoglist/>
                                                <SCCall>false</SCCall>
                                                <ReturnLoglist>false</ReturnLoglist>
                                                <StokSil>false</StokSil>
                                                <CustomData>
                                                    <ArrayOfClsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>LNGYIL</Key>
                                                            <Value xsi:type="xsd:int">2018</Value>
                                                        </clsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>LNGDISTKOD</Key>
                                                            <Value xsi:type="xsd:int">12</Value>
                                                        </clsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>TXTDISTKOD</Key>
                                                            <Value xsi:type="xsd:string">SH012</Value>
                                                        </clsDictionaryOfStringObject>
                                                        <clsDictionaryOfStringObject>
                                                            <Key>TXTSTKOD</Key>
                                                            <Value xsi:type="xsd:string">SH010</Value>
                                                        </clsDictionaryOfStringObject>
                                                    </ArrayOfClsDictionaryOfStringObject>
                                                </CustomData>
                                            </ResultEntitySet>
                                        </IntegrationGetEntitySetWithPacketLoginResult>
                                    </IntegrationGetEntitySetWithPacketLoginResponse>
                                </soap:Body>
                            </soap:Envelope>

        Dim doc = New XDocument(x)

        Dim nt As NameTable = New NameTable()
        Dim nsmgr As XmlNamespaceManager = New XmlNamespaceManager(nt)
        nsmgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/")
        nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
        nsmgr.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema")
        nsmgr.AddNamespace("univera", "http://integration.univera.com.tr")

        Dim a = doc.XPathSelectElements("/soap:Envelope/soap:Body/IntegrationGetEntitySetWithPacketLoginResponse/IntegrationGetEntitySetWithPacketLoginResult/ResultEntitySet/CustomData/ArrayOfClsDictionaryOfStringObject/clsDictionaryOfStringObject", nsmgr)
        Dim lngyil2 = (
                       From v In a
                       Where v.XPathSelectElement("Key").Value = "LNGYIL"
                       Select v.XPathSelectElement("Value").Value
                     ).Single()

        Console.WriteLine(lngyil2)

        Console.ReadLine()

    End Sub

End Module

More work on the XPath query would let you get the value directly without the loop after it.


Refs:

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84