473,503 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Traversing XML with Linq to XML - newbie q

Hi

I am struggling to navigate an XElement and I'm not sure what is going
wrong. Here's my XML via vb's xml literals

Dim l_testSchema = _
<Mappings>
<Definitions>
<ItemGroup>
<Parent>Table</Parent>
<Name>Name</Name>
</ItemGroup>
<Item>
<Parent>Column</Parent>
<Name>Name</Name>
<Type>Type</Type>
</Item>
</Definitions>

<Data>
<Table>
<Name>MyTable</Name>
<Columns>
<Column>
<Name>MyColumn1</Name>
<Type>String</Type>
</Column>
<Column>
<Name>MyColumn2</Name>
</Column>
<Column>
<Name>MyColumn3</Name>
</Column>
<Column>
<Name>MyColumn4</Name>
</Column>
<Column>
<Name>MyColumn5</Name>
</Column>
<Column>
<Name>MyColumn6</Name>
</Column>
</Columns>
</Table>
</Data>
</Mappings>

I'm basically wanting to use linq to traverse this but I'm struggling to
get anything back

e.g.
l_testSchema.Elements("Columns").Count returns 0
l_testSchema.Descendents(Columns").Count returns 1

In fact I'm struggling to get any other result other than 0 or 1.

Why is that the case? What am I missing?

Thx

Simon
Oct 30 '08 #1
2 1195
OK ... I didn't realise I needed to put in the absolute path

l_testSchema.Element("Data").Element("Table").Elem ent("Columns").Elements.Count

I thought it would just 'jump' to the correct element.

Thx

Simon Woods wrote:
Hi

I am struggling to navigate an XElement and I'm not sure what is going
wrong. Here's my XML via vb's xml literals

Dim l_testSchema = _
<Mappings>
<Definitions>
<ItemGroup>
<Parent>Table</Parent>
<Name>Name</Name>
</ItemGroup>
<Item>
<Parent>Column</Parent>
<Name>Name</Name>
<Type>Type</Type>
</Item>
</Definitions>

<Data>
<Table>
<Name>MyTable</Name>
<Columns>
<Column>
<Name>MyColumn1</Name>
<Type>String</Type>
</Column>
<Column>
<Name>MyColumn2</Name>
</Column>
<Column>
<Name>MyColumn3</Name>
</Column>
<Column>
<Name>MyColumn4</Name>
</Column>
<Column>
<Name>MyColumn5</Name>
</Column>
<Column>
<Name>MyColumn6</Name>
</Column>
</Columns>
</Table>
</Data>
</Mappings>

I'm basically wanting to use linq to traverse this but I'm struggling to
get anything back

e.g.
l_testSchema.Elements("Columns").Count returns 0
l_testSchema.Descendents(Columns").Count returns 1

In fact I'm struggling to get any other result other than 0 or 1.

Why is that the case? What am I missing?

Thx

Simon
Oct 30 '08 #2


l_testSchema.Descendants("Column").Count gives me what I was after.

Thx
Simon Woods wrote:
Hi

I am struggling to navigate an XElement and I'm not sure what is going
wrong. Here's my XML via vb's xml literals

Dim l_testSchema = _
<Mappings>
<Definitions>
<ItemGroup>
<Parent>Table</Parent>
<Name>Name</Name>
</ItemGroup>
<Item>
<Parent>Column</Parent>
<Name>Name</Name>
<Type>Type</Type>
</Item>
</Definitions>

<Data>
<Table>
<Name>MyTable</Name>
<Columns>
<Column>
<Name>MyColumn1</Name>
<Type>String</Type>
</Column>
<Column>
<Name>MyColumn2</Name>
</Column>
<Column>
<Name>MyColumn3</Name>
</Column>
<Column>
<Name>MyColumn4</Name>
</Column>
<Column>
<Name>MyColumn5</Name>
</Column>
<Column>
<Name>MyColumn6</Name>
</Column>
</Columns>
</Table>
</Data>
</Mappings>

I'm basically wanting to use linq to traverse this but I'm struggling to
get anything back

e.g.
l_testSchema.Elements("Columns").Count returns 0
l_testSchema.Descendents(Columns").Count returns 1

In fact I'm struggling to get any other result other than 0 or 1.

Why is that the case? What am I missing?

Thx

Simon

Oct 30 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4862
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the...
2
1300
by: Satish | last post by:
Hi I an newbie to LINQ. I have a dataset with one table employee which has a self referencing foreign key manger id. I want to write a query to get all the employees under a particular manager....
30
4248
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
1
1515
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain fields which are in the form of combo box in the...
3
1065
by: zacks | last post by:
I am trying to get into LINQ. I have created ConsoleApplication project and have pasted in the code from one of the examples in help: DataContext db = new DataContext(@"c:\program files...
10
1190
by: damiensawyer | last post by:
Hi, I have the following code extract... ---top--- List<PersonPersons = new List<Person>(); Persons.Add(new Person("Peter", 28,"Perth")); Persons.Add(new Person("Matthew", 31,...
5
1133
by: Mike P | last post by:
Here is another one I hope somebody can help me with, this is the AnyGrouped example and does not recognise the 'Group' as being valid : List<Productproducts = GetProductList(); var...
1
2582
by: DaveD | last post by:
Let's say I have a collection ("items") of objects that each have a "string Name" property. I understand I can create a loop like this: foreach (string name in names) { var item = (from x in...
2
2749
by: Simon Woods | last post by:
Hi I am struggling to navigate an XElement and I'm not sure what is going wrong. Here's my XML via vb's xml literals Dim l_testSchema = _ <Mappings> <Definitions> <ItemGroup>...
0
1065
by: balint kardos | last post by:
Hi all, My question is about parsing and saving a LINQ query from/to xml. I'm creating an web interface where a user can select fields from dataset tables, and create relations between them...
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7468
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5023
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.