0

我有一些带有如下标记的 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") 中的内容,这只是为了描述我想要的......

4

1 回答 1

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调试器中的内容:

在此处输入图像描述

于 2013-06-04T13:06:05.937 回答