Question X path......

Benny33

New member
Joined
Jul 3, 2011
Messages
2
Programming Experience
Beginner
Hey guys,I'm new to the forums and Have a little bit of coding experience .
But I have encounted a problem ,which I was wondering if you guys could help me out with.

I'm creating a program in which I need to extract the element field names and list them in a listbox
Heres a example of what I'm talking about

HTML:
<Root>
  <PersonBob>
    <Name>Bob</Name>
    <Age>28</Age>
    <Weight>180</Weight>
  </PersonBob>
  <PersonJulie>
    <Name>Julie</Name>
    <Age>34</Age>
    <Weight>125</Weight>
  </PersonJulie>
  <PersonJack>
    <Name>Jack</Name>
    <Age>34</Age>
    <Weight>125</Weight>
  </PersonJack>
  <PersonBen>
    <Name>Ben</Name>
    <Age>21</Age>
    <Weight>121</Weight>
  </PersonBen>
</Root>
So I want to select All Children of the Root element nothing else .

So my items in the Listbox should be the following

  • PersonBob
  • PersonJulie
  • PersonJack
  • PersonBen
nothing more nothing less.
Note:that this is a example I have about 30 fields I want to add.
What properties should I use and what Xpath code should I use .
Please post code


Cheers
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,324
Location
Norway
Programming Experience
10+
Me.ListBoxNames.DataSource = (From el In XDocument.Load("file.xml").<Root>.Elements Select el.Name).ToArray
 

Benny33

New member
Joined
Jul 3, 2011
Messages
2
Programming Experience
Beginner
Thanks for your reply it works
but
in my xml file
I have lots of the same element so the element is repeated multiple times .How do I get it to only have one entrie for one element
 

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,324
Location
Norway
Programming Experience
10+
Use Distinct in query or Distinct function, eg:
...Select el.Name Distinct
or
...Select el.Name).Distinct.ToArray

The data structure you posted is really horrible by the way, but I guess it is educationally. :rolleyes:
 
Top Bottom