[英]Selecting multiple values from XML XDocument (VB.NET)
I have some XML-files with markup something like this: 我有一些带有标记的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>
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. 在实际情况下,组ID:s更复杂,因此我希望不必对元素进行计数,而是希望通过其元素属性来选择它们。 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... 编辑说明:这不是我真正的查询尝试,也不是Duck.Element(“ blah WHERE blah”)中的某些内容,仅用于描述我想要的...
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: 编辑:您可以检查v
在调试器中:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.