all groups > dotnet xml > july 2007 >
You're in the

dotnet xml

group:

Hpw to know level tag position



Hpw to know level tag position sam
7/22/2007 12:00:00 AM
dotnet xml: I need to know a way for knowing tag position in a xml like this:

<AAA>
<BBB>
<CCC>
<DDD_1/>
<DDD_2/>
<DDD_3/>
</CCC>
<BBB/>
</AAA>
I need to get that AAA = 1, BBB=2,CCC=3,
DDD_1=4, DDD_2=4,DDD_3=4

thanks for help.
Sam

Re: Hpw to know level tag position Martin Honnen
7/22/2007 12:00:00 AM
[quoted text, click to view]

count($n/ancestor::node()) where $n is your node. So with a stylesheet
you could do:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="text"/>

<xsl:template match="/">
<xsl:apply-templates select="//*"/>
</xsl:template>

<xsl:template match="*">
<xsl:value-of select="concat(name(), ' = ',
count(./ancestor::node()))"/>
<xsl:if test="position() != last()"><xsl:text>, </xsl:text></xsl:if>
</xsl:template>

</xsl:stylesheet>


--

Martin Honnen --- MVP XML
Re: Hpw to know level tag position sam
7/22/2007 7:19:40 PM
[quoted text, click to view]
Okey, but I am using XML .Net control for handling xml files.
So, how to get this value in csharp code ?

Sam

"Martin Honnen" <mahotrash@yahoo.de> a écrit dans le message de news:
Oru9qNFzHHA.1184@TK2MSFTNGP04.phx.gbl...
[quoted text, click to view]

Re: Hpw to know level tag position Martin Honnen
7/23/2007 3:13:45 PM
[quoted text, click to view]

Using System.Xml.XmlDocument you can do it like this:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
foreach (XmlNode element in xmlDocument.SelectNodes(@"//*"))
{
Console.WriteLine("{0} = {1}.", element.Name,
element.SelectNodes(@"ancestor::node()").Count);
}

If you are using the ASP.NET XML control then use the stylesheet I
posted in my first reply.


--

Martin Honnen --- MVP XML
Re: Hpw to know level tag position sam
7/23/2007 5:06:26 PM
Martin, thank you very very very much for all samples that works fine.

Sam

AddThis Social Bookmark Button