简体   繁体   中英

Selecting multiple values from XML XDocument (VB.NET)

I have some XML-files with markup something like this:

<?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>

What i want to do is to select only some of these values for output.

If i have code like this:

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

I get the name(s) of the ducks, but let's say i want to get the cell phone number, home phone number and status, how can i then grab the value from an element where the attribute is equal to something?

In the real case the group ID:s are more complex so i would prefer to not have to count elements, i would like to select them by their element attributes. Like this:

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

I've tried some queries but can't really got the hang of it. Any suggestions?

Edit note: That's not my real tries for queries or something there in the Duck.Element("blah WHERE blah"), that's just for describing what i want...

Here is an example:

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

Works for your test case:

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>

EDIT: You can examine what v is in debugger:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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