我一直在尝试解析并从xml中提取一些值(以字符串开头),这是我从Web服务中检索到的,但我正在努力迭代它。这是xml字符串的格式(抱歉它有点密集):
<?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>
我当时试图获取每个报告的名称,然后获取其余值,但我不确定为什么它不起作用。这是我一直试图开始工作的代码:
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
这是我试图模仿的答案,因为它似乎最适合我需要的东西: LINQ to XML in VB.NET
答案 0 :(得分:0)
您缺少命名空间:
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
使用XML,如果你有一个带前缀的元素,例如soap:Envelope
,你需要在XML的根目录中查找xmlns:soap
的名称空间http://schemas.xmlsoap.org/soap/envelope/
。对于非前缀(在您的情况下),请查看最近的父元素,即EDPReportPage
。
此处有关此主题的更多解释:
答案 1 :(得分:0)
如果您希望使用命名空间的XML文字,则必须导入命名空间。
在模块的顶部,声明命名空间:
Imports <xmlns:ns="http://www.NationalGrid.com/EDP/BusinessEntities/Public">
然后您的查询变为:
Dim names =
From obj in xml...<ns:EDPObjectBE>
Select obj.<ns:EDPObjectName>.Value