本文介绍了将XML转换为VB.NET字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用LINQ将来自XML的子子值放入字典集合中。我使用与XML相同的结构的列表和自定义集合来完成此操作,但无法搜索特定的值。如果我知道parentName,childName和subChildName,我希望能够找到subChildProperty1.value和subChildProperty2.value,而不需要遍历整个集合和每个后续的子集合,就像我与列表一样。这可能不是最好的实现方式,并且可以接受建议,但仍然想知道如何使这项工作成为可能。这将允许我有一个字典项目是:

  key =parentNameValue1.childNameValue1.subchildNameValue1.subChildProperty1
值= 0

我可以连接字符串形成一个特定的键,并在该键上搜索返回一个值。



XML:

 < root> 
< parent>
< parentName> parentNameValue1< / parentName>
< child>
< childName> childNameValue1< / childName>
< subchild>
< subchildName> subchildNameValue1< / subchildName>
< subChildProperty1> 0< / subChildProperty1>
< subChildProperty2> 5< / subChildProperty2>
< / subchild>
< subchild>
< subchildName> subchildNameValue2< / subchildName>
< subChildProperty1> 0< / subChildProperty1>
< subChildProperty2> 10< / subChildProperty2>
< / subchild>
< / child>
< / parent>
< root>

这个问题有点类似于,但是我无法在VB中为我的应用程序使用代码。



我是新来的SO(和VB),所以我道歉,如果我的礼节是不正确的。

解决方案

使用VB.NET的极好的XML支持,这很容易做到:

  Imports System.Xml.Linq 

...

Sub Main()
Dim xml = _
< root>
< parent>
< parentName> parentNameValue1< / parentName>
< child>
< childName> childNameValue1< / childName>
< subchild>
< subchildName> subchildNameValue1< / subchildName>
< subChildProperty1> 0< / subChildProperty1>
< subChildProperty2> 5< / subChildProperty2>
< / subchild>
< subchild>
< subchildName> subchildNameValue2< / subchildName>
< subChildProperty1> 0< / subChildProperty1>
< subChildProperty2> 10< / subChildProperty2>
< / subchild>
< / child>
< / parent>
< / root>

'或者,从文件加载XML
'Dim xml = XDocument.Load(fileName)

Dim dict作为新字典(Of String,Integer)

'提取属性
对于每个父项在xml。< parent>
Dim parentName = parent。< parentName> .Value
对于每个子项在父项中。< child>
Dim childName = child。< childName> .Value
For each subchild In child。< subchild>
Dim subchildName = subchild。< subchildName> .Value
For each prop In subchild.Elements.Where(Function(e)e.Name<>subchildName)
dict。添加(String.Format({0}。{1}。{2}。{3},parentName,childName,subchildName,prop.Name),_
Integer.Parse(prop.Value) b $ b下一个
下一个
下一个
下一个

'打印值,表明我们做得很好
对于每个kv In dict
Console.WriteLine({0}:{1},kv.Key,kv.Value)
下一个
Console.ReadLine()
End Sub

p>

I'm trying to put sub-child values from XML into a dictionary collection using LINQ. I've done this with lists and custom collections which follow the same structure as the XML but am unable to search for specific values. If I know parentName, childName, and subChildName I want to be able to find subChildProperty1.value and subChildProperty2.value without iterating through the entire collection and each of the subsequent sub-collections, as I have to do with lists. This may not be the best implementation, and am open to suggestions, but still would like to figure out how to make this work. This would then allow me to have a dictionary item be:

key = "parentNameValue1.childNameValue1.subchildNameValue1.subChildProperty1"
value = 0

and I could just concatenate strings to form a specific key and search on that key to return a value.

XML:

<root>
    <parent>
        <parentName>parentNameValue1</parentName>
        <child>
            <childName>childNameValue1</childName>
            <subchild>
                <subchildName>subchildNameValue1</subchildName>
                <subChildProperty1>0</subChildProperty1>
                <subChildProperty2>5</subChildProperty2>
            </subchild>
            <subchild>
                <subchildName>subchildNameValue2</subchildName>
                <subChildProperty1>0</subChildProperty1>
                <subChildProperty2>10</subChildProperty2>
            </subchild>
        </child>
    </parent>
<root>

This question is somewhat similar to this question here but I could not get the code working in VB for my application.

I am new to SO (and VB) so I apologize if my etiquette is incorrect.

解决方案

With VB.NET's great XML support, this is quite easy to do:

Imports System.Xml.Linq

...

Sub Main()
    Dim xml = _
        <root>
            <parent>
                <parentName>parentNameValue1</parentName>
                <child>
                    <childName>childNameValue1</childName>
                    <subchild>
                        <subchildName>subchildNameValue1</subchildName>
                        <subChildProperty1>0</subChildProperty1>
                        <subChildProperty2>5</subChildProperty2>
                    </subchild>
                    <subchild>
                        <subchildName>subchildNameValue2</subchildName>
                        <subChildProperty1>0</subChildProperty1>
                        <subChildProperty2>10</subChildProperty2>
                    </subchild>
                </child>
            </parent>
        </root>

    ' Alternatively, load XML from a file
    ' Dim xml = XDocument.Load(fileName)

    Dim dict As New Dictionary(Of String, Integer)

    ' Extract the properties
    For Each parent In xml.<parent>
        Dim parentName = parent.<parentName>.Value
        For Each child In parent.<child>
            Dim childName = child.<childName>.Value
            For Each subchild In child.<subchild>
                Dim subchildName = subchild.<subchildName>.Value
                For Each prop In subchild.Elements.Where(Function(e) e.Name <> "subchildName")
                    dict.Add(String.Format("{0}.{1}.{2}.{3}", parentName, childName, subchildName, prop.Name), _
                             Integer.Parse(prop.Value))
                Next
            Next
        Next
    Next

    ' Print the values, to show that we've done a good job
    For Each kv In dict
        Console.WriteLine("{0}: {1}", kv.Key, kv.Value)
    Next
    Console.ReadLine()
End Sub

这篇关于将XML转换为VB.NET字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:39