繁体   English   中英

使用VB.NET将Linq转换为XML

[英]Linq to XML with VB.NET

我正在尝试处理Linq to XML-来自MSXML。 我想循环使用以下XML包含在'Root'中的所有元素,返回元素名称和每个属性的值。

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>

例如,对于循环的每次迭代,我希望能够填充以下变量:

MonthName = Taken from the element name
Target = Taken from the attribute value
Forecast = Taken from the attribute value
Result = Taken from the attribute value

我设法按如下方式加载了文档:

Dim xDoc As New XDocument
        xDoc = XDocument.Load("C:\Users\Major\Documents\Visual Studio 2013\Dashboard\XML\YTD - Copy.xml")

但是我能找到的所有代码都与获取具有特定名称的元素有关,在这里我想循环所有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

您可以使用以下方法:

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()

然后,您可以简单地使用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

感谢所有的答案。 我找到的最简单的路线如下:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM