Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting multiple values from XML XDocument (VB.NET)
    text
    copied!<p>I have some XML-files with markup something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;clients&gt; &lt;client id="00000" name="Donald Duck"&gt; &lt;group id="AAA" name="ClientInfo"&gt; &lt;term id="Sex"&gt;Male&lt;/term&gt; &lt;term id="Status"&gt;In a relationship&lt;/term&gt; &lt;/group&gt; &lt;group id="BBB" name="ClientTelephoneNumbers"&gt; &lt;term id="Home"&gt;0000-0000000&lt;/term&gt; &lt;term id="Cell"&gt;1111-1111111&lt;/term&gt; &lt;/group&gt; &lt;group id="CCC" name="WorkingStatus"&gt; &lt;term id="HasAJob"&gt;Yes&lt;/term&gt; &lt;term id="Where"&gt;Somewhere&lt;/term&gt; &lt;/group&gt; &lt;/client&gt; &lt;client id="11111" name="Daisey Duck"&gt; &lt;group id="AAA" name="ClientInfo"&gt; &lt;term id="Sex"&gt;Female&lt;/term&gt; &lt;term id="Status"&gt;In a relationship&lt;/term&gt; &lt;/group&gt; &lt;group id="BBB" name="ClientTelephoneNumbers"&gt; &lt;term id="Home"&gt;2222-2222222&lt;/term&gt; &lt;term id="Cell"&gt;3333-3333333&lt;/term&gt; &lt;/group&gt; &lt;group id="CCC" name="WorkingStatus"&gt; &lt;term id="HasAJob"&gt;Unknown&lt;/term&gt; &lt;term id="Where"&gt;Unknown&lt;/term&gt; &lt;/group&gt; &lt;/client&gt; &lt;/clients&gt; </code></pre> <p>What i want to do is to select only some of these values for output.</p> <p>If i have code like this:</p> <pre><code>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 </code></pre> <p>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?</p> <p>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:</p> <pre><code>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 </code></pre> <p>I've tried some queries but can't really got the hang of it. Any suggestions?</p> <p>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...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload