从XML XDocument(VB.NET)中选择多个值

时间:2013-06-04 12:30:03

标签: xml vb.net linq linq-to-xml

我有一些带有标记的XML文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<clients>
    <client id="00000" name="Donald Duck">
        <group id="AAA" name="ClientInfo">
            <term id="Sex">Male</term>
            <term id="Status">In a relationship</term>
        </group>
        <group id="BBB" name="ClientTelephoneNumbers">
            <term id="Home">0000-0000000</term>
            <term id="Cell">1111-1111111</term>
        </group>
        <group id="CCC" name="WorkingStatus">
            <term id="HasAJob">Yes</term>
            <term id="Where">Somewhere</term>
        </group>
    </client>
    <client id="11111" name="Daisey Duck">
        <group id="AAA" name="ClientInfo">
            <term id="Sex">Female</term>
            <term id="Status">In a relationship</term>
        </group>
        <group id="BBB" name="ClientTelephoneNumbers">
            <term id="Home">2222-2222222</term>
            <term id="Cell">3333-3333333</term>
        </group>
        <group id="CCC" name="WorkingStatus">
            <term id="HasAJob">Unknown</term>
            <term id="Where">Unknown</term>
        </group>
    </client>
</clients>

我想要做的是只选择其中一些值作为输出。

如果我有这样的代码:

Dim xml As XDocument = Xdocument.Load(ducks.xml)
For Each Duck As XElement in xml.Descendants("client")
    Dim Name As String = Duck.Attribute("Name").Value
Next

我得到了鸭子的名字,但是假设我想获取手机号码,家庭电话号码和状态,那么我怎样才能从属性等于某个元素的元素中获取值?

在实际情况中,组ID:s更复杂,所以我宁愿不必计算元素,我想按元素属性选择它们。像这样:

Dim xml As XDocument = Xdocument.Load(ducks.xml)
For Each Duck As XElement in xml.Descendants("client")
    Dim Name As String = Duck.Attribute("Name").Value
    Dim Cellphone As string = Duck.Element("group WHERE id IS BBB").Element("term WHERE id IS Cell").Value
    Dim Homephone As string = Duck.Element("group WHERE id IS BBB").Element("term WHERE id IS Home").Value
Next

我已经尝试了一些查询,但不能真正掌握它。有什么建议吗?

编辑注释:这不是我真正尝试查询或Duck.Element中的某些东西(“blah WHERE blah”),这只是为了描述我想要的东西......

1 个答案:

答案 0 :(得分:0)

以下是一个例子:

Dim v = From clt In xml.<client>
        Where clt.@name = "Donald Duck"
        Select cellPhone = clt...<term>.Where(Function(x) x.@id = "Cell").Value,
               homePhone = clt...<term>.Where(Function(x) x.@id = "Home").Value,
               status = clt...<term>.Where(Function(x) x.@id = "Status").Value

适用于您的测试用例:

Dim xml = <clients>
            <client id="00000" name="Donald Duck">
              <group id="AAA" name="ClientInfo">
                <term id="Sex">Male</term>
                <term id="Status">In a relationship</term>
              </group>
              <group id="BBB" name="ClientTelephoneNumbers">
                <term id="Home">0000-0000000</term>
                <term id="Cell">1111-1111111</term>
              </group>
              <group id="CCC" name="WorkingStatus">
                <term id="HasAJob">Yes</term>
                <term id="Where">Somewhere</term>
              </group>
            </client>
            <client id="11111" name="Daisey Duck">
              <group id="AAA" name="ClientInfo">
                <term id="Sex">Female</term>
                <term id="Status">In a relationship</term>
              </group>
              <group id="BBB" name="ClientTelephoneNumbers">
                <term id="Home">2222-2222222</term>
                <term id="Cell">3333-3333333</term>
              </group>
              <group id="CCC" name="WorkingStatus">
                <term id="HasAJob">Unknown</term>
                <term id="Where">Unknown</term>
              </group>
            </client>
          </clients>

编辑:您可以查看调试器中v的内容:

enter image description here