[英]Linq to XML with VB.NET
I'm trying to get to grips with Linq to XML - coming from MSXML. 我正在尝试处理Linq to XML-来自MSXML。 I would like to loop all elements contained within 'Root' in the following XML, returning the element name, and values for each of the attributes. 我想循环使用以下XML包含在'Root'中的所有元素,返回元素名称和每个属性的值。
XML: XML:
<Root>
<April Target="55" Forecast="30" Result="30"/>
<May Target="98" Forecast="67" Result="84"/>
<June Target="133" Forecast="109" Result="119"/>
<July Target="183" Forecast="146" Result="150"/>
<August Target="239" Forecast="182" Result="188"/>
<September Target="278" Forecast="211" Result="217"/>
<October Target="320" Forecast="251" Result="489"/>
<November Target="372" Forecast="281" Result="489"/>
<December Target="386" Forecast="324" Result="489"/>
<January Target="420" Forecast="352" Result="489"/>
<February Target="464" Forecast="384" Result="489"/>
<March Target="524" Forecast="445" Result="489"/>
</Root>
For example, for each iteration of the loop, I would like to be able to populate the following variables: 例如,对于循环的每次迭代,我希望能够填充以下变量:
MonthName = Taken from the element name
Target = Taken from the attribute value
Forecast = Taken from the attribute value
Result = Taken from the attribute value
I have managed to load the document as follows: 我设法按如下方式加载了文档:
Dim xDoc As New XDocument
xDoc = XDocument.Load("C:\Users\Major\Documents\Visual Studio 2013\Dashboard\XML\YTD - Copy.xml")
But all code I can find relates to getting elements with specific names, where I want to loop all children of Root 但是我能找到的所有代码都与获取具有特定名称的元素有关,在这里我想循环所有Root的子代
Dim xDoc = <Root>
<April Target="55" Forecast="30" Result="30"/>
<May Target="98" Forecast="67" Result="84"/>
<June Target="133" Forecast="109" Result="119"/>
<July Target="183" Forecast="146" Result="150"/>
<August Target="239" Forecast="182" Result="188"/>
<September Target="278" Forecast="211" Result="217"/>
<October Target="320" Forecast="251" Result="489"/>
<November Target="372" Forecast="281" Result="489"/>
<December Target="386" Forecast="324" Result="489"/>
<January Target="420" Forecast="352" Result="489"/>
<February Target="464" Forecast="384" Result="489"/>
<March Target="524" Forecast="445" Result="489"/>
</Root>
For Each node In xDoc.Elements()
Console.WriteLine(node.Name)
For Each attr In node.Attributes
Console.WriteLine("{0} = {1}", attr.Name, attr.Value)
Next
Next
You can use this:- 您可以使用以下方法:
Dim xdoc As XDocument = XDocument.Load("Your_Data.xml")
Dim result = (From d In xdoc.Root.Elements()
Select New With {.MonthName = d.Name.LocalName,
.Target = d.Attribute("Target").Value,
.Forecast = d.Attribute("Forecast").Value,
.Result = d.Attribute("Result").Value}).ToList()
Then you can simply use the For Each
loop to iterate through the result:- 然后,您可以简单地使用For Each
循环遍历结果:-
For Each output In result
Console.WriteLine("Month: {0},Target: {1}, Forecast: {2}, Result: {3}", output.MonthName, output.Target, output.Forecast, output.Result)
Next
Thanks to all for the answers. 感谢所有的答案。 The route I've found easiest is as follows: 我找到的最简单的路线如下:
xDoc = XDocument.Load("XML Path Here")
Dim MonthNodes As IEnumerable(Of XElement) = xDoc.Element("Root").Descendants
For Each MonthNode As XElement In MonthNodes
Debug.Print(MonthNode.Attribute("Target").Value)
Next
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.