1

I have been trying to parse and extract some values from xml (starts off as a string) which I retrieved from a web service but i'm struggling to iterate through it. Here is the format of the xml string (sorry it's a bit dense):

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <GetInstantaneousFlowDataResponse xmlns="http://www.NationalGrid.com/EDP/UI/">
      <GetInstantaneousFlowDataResult>
        <ReportName xmlns="http://www.NationalGrid.com/EDP/BusinessEntities/Public">string</ReportName>
        <PublishedTime xmlns="http://www.NationalGrid.com/EDP/BusinessEntities/Public">dateTime</PublishedTime>
        <EDPReportPage xmlns="http://www.NationalGrid.com/EDP/BusinessEntities/Public">
          <PageName>string</PageName>
          <CurrentGasDay>dateTime</CurrentGasDay>
          <EDPEnergyGraphTableCollection>
            <EDPEnergyGraphTableBE xsi:nil="true" />
             <ItemPosition>1</ItemPosition>
                     <EDPObjectCollection>                        
                        <EDPObjectBE>
                           <EDPObjectName>ALDBROUGH</EDPObjectName>
                           <EnergyDataList>
                              <EDPEnergyDataBE>
                                 <ApplicableAt>2014-09-24T12:00:00</ApplicableAt>
                                 <FlowRate>0</FlowRate>
                                 <QualityIndicator />
                                 <ScheduleTime>2014-09-24T12:12:00</ScheduleTime>
                              </EDPEnergyDataBE>
                           </EnergyDataList>
                        </EDPObjectBE>
            <EDPEnergyGraphTableBE xsi:nil="true" />
          </EDPEnergyGraphTableCollection>
          <NoteCollection>
            <EDPNoteBE xsi:nil="true" />
            <EDPNoteBE xsi:nil="true" />
          </NoteCollection>
        </EDPReportPage>
      </GetInstantaneousFlowDataResult>
    </GetInstantaneousFlowDataResponse>
  </soap:Body>
</soap:Envelope>

I was trying to get just the name of each report for the moment then getting the rest of the values later but i'm not sure why it isn't working. This is the code I have been trying to get to work:

    Dim xmlDoc As XDocument = XDocument.Parse(xml)

    Dim reports = From flowData In xmlDoc...<EDPObjectBE> _
            Select flowData 

    For Each flowData In reports
        MsgBox(flowData .<EDPObjectName>.Value)
    Next

Here is the answer that I was trying to emulate as it seemed most suitable for what I need: LINQ to XML in VB.NET

Community
  • 1
  • 1
Badumtsh
  • 45
  • 3

2 Answers2

0

You are missing a namespace:

Dim ns As XNamespace =
  XNamespace.Get("http://www.NationalGrid.com/EDP/BusinessEntities/Public")

For Each e As XElement In xmlDoc.Descendants(ns + "EDPObjectBE")
  Console.WriteLine(e.Element(ns + "EDPObjectName").Value)
Next

With XML, if you have a prefixed element, such as soap:Envelope, you need to be looking for namespace of xmlns:soap, which is http://schemas.xmlsoap.org/soap/envelope/, declared at the root in your XML. For non-prefixed, which is your case, look at the nearest parent element, which is EDPReportPage.

More explanation on the topic here:

Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
0

If you wish to use namespaced XML literals, you have to import the namespace.

At the top of your module, declare the namespace:

Imports <xmlns:ns="http://www.NationalGrid.com/EDP/BusinessEntities/Public">

Then your query becomes:

Dim names =
    From obj in xml...<ns:EDPObjectBE>
    Select obj.<ns:EDPObjectName>.Value
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272