473,406 Members | 2,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to keep track of node levels when we Linq an XML document?

I have an xml document that looks *like* so:

<Mammal name="Cat">
<Mammal name="African Tigers" >
<Mammal name="Central African Tiger" />
<Mammal name="Ethiopian Tiger" />
</Mammal>
<Mammal name="Asian Tigers" >
<Mammal name="Indian Tiger" />
<Mammal name="China Tigers" >
<Mammal name="South China Tiger" />
<Mammal name="Northeast China Tiger" />
</Mammal>
</Mammal>
</Mammal>

Well, I understand that this XML can be improved, but I don't have the
option. I have to work on this coarsely designed XML document.

Now, I want to process this XML document and output the list of cats
and keep their hierarchical info like so (I hope the indentation won't
get messed up while you are reading this):

Cat
African Tigers
Central African Tiger
Ethiopian Tiger
Asian Tigers
Indian Tiger
China Tigers
South China Tiger
Northeast China Tiger

I have a way to do this with the traditional XmlDocument API. But, I
cannot figure out how to do this with the Linq XDocument.

I have the following code:

XDocument xd = XDocument.Load(new XmlNodeReader(node));

var cats = from cat in xd.Descendants("Cat") select cat;

foreach (var c in cats)
{
Console.WriteLine(Convert.ToString(c.Attribute("na me").Value));
}

This will simply output as this:

Cat
African Tigers
Central African Tiger
Ethiopian Tiger
Asian Tigers
Indian Tiger
China Tigers
South China Tiger
Northeast China Tiger

So, my question is: With Linq to Xml, how do I keep track of the node
level information so that I can add numbers of "\t" (the tab) to the
WriteLine method accordingly?

I hope that I have made my question clear. Thanks a lot.

Sep 4 '08 #1
1 3231
Author <gn********@gmail.comwrote:

<snip>
So, my question is: With Linq to Xml, how do I keep track of the node
level information so that I can add numbers of "\t" (the tab) to the
WriteLine method accordingly?

I hope that I have made my question clear. Thanks a lot.
Try node.Ancestors().Count()

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 4 '08 #2

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

Similar topics

12
by: pillepop2003 | last post by:
Hey! Can anyone give me a hint, how this problem is best implemented: I have a table of users (see below), where every user has one "superior user" (= parent node), this should be a fully...
14
by: neerajb | last post by:
Hi, I am having an XML document(input.xml) which is showing the menu heirarchy used in my application.My requirement is to add "submenu" tag to those menuitems who are having the child menuitems...
15
by: l3vi | last post by:
I have a new system Im building that stores entries of what people are searching for on my sites. I want to be able to keep records of how many times a keyword was searched for daily, and from...
6
by: datamodel | last post by:
Hello I have an XML tree of which you can see a mini-version here: http://paste.uni.cc/11838 (the tree is actually over 30,000 levels deep) How do I count the depth of a given...
0
by: Guzeppi | last post by:
Hi, i'm using linq to load an xml structure into my classes. the xml consists of the same node nested for multiple levels e.g. <node id="node_id01" name="node 01"> <node id="node_id0101"...
9
by: Thom Little | last post by:
Using C# 3.5 I want to select a value from an XML file. The intent of the following is to print "EpsilonGamma". It does not work. Can you point out my probably very obvious error? XmlDocument...
2
by: shapper | last post by:
Hello, I am trying to get the node "album" in a XML file given its "id" attribute: <gallery> <album id = "1" title="Intro" ....
4
by: Christiano Donke | last post by:
Hey there... I intend to make a WindowsApp menu like... For that I need to read a XML file node by node to try to create (at runtime) the items... Where can I find some clear information on...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.