469,330 Members | 1,329 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

How to retrieve XML sublevel, using GetElementsByTagName

Hello,

I have the following example XML:
<data>
<package>
<packageid>123</packageid>
<package_article>
<articleid>article1</articleid>
</package_article>
</package>

<package>
<packageid>456</packageid>
<package_article>
<articleid>article2</articleid>
</package_article>
</package>
</data>

I want to be able to list the following to the client, based on the above xml:
Package: 123
Article: article1
Package: 456
Article: article2

I can successfullt retrieve the information located in the <package> node by
using the following code:
myList = xmlDoc.GetElementsByTagName("package")
For Each Node In myList
....
next

But how can I recieve information from the subnode package_article, inside
the above loop?
Nov 12 '05 #1
3 32864


Andy wrote:

I have the following example XML:
<data>
<package>
<packageid>123</packageid>
<package_article>
<articleid>article1</articleid>
</package_article>
</package>

<package>
<packageid>456</packageid>
<package_article>
<articleid>article2</articleid>
</package_article>
</package>
</data>

I want to be able to list the following to the client, based on the above xml:
Package: 123
Article: article1
Package: 456
Article: article2


GetElementsByTagName is not only a method of the document itself but of
any element node so you can use that as follows (C# code):

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005070301.xml");
foreach (XmlNode node in xmlDocument.GetElementsByTagName("package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.GetElementsByTagName("packageid")[0];
if (packageid != null) {
Console.WriteLine("Package: {0}", packageid.InnerText);
}
XmlElement articleid = (XmlElement)
package.GetElementsByTagName("articleid")[0];
if (articleid != null) {
Console.WriteLine("Article: {0}", articleid.InnerText);
}
Console.WriteLine();
}

But as .NET implements XPath it is usually easier and more elegant to
solve such tasks with XPath:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005070301.xml");
foreach (XmlNode node in xmlDocument.SelectNodes("/data/package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.SelectSingleNode("packageid");
if (packageid != null) {
Console.WriteLine("Package: {0}", packageid.InnerText);
}
XmlElement articleid = (XmlElement)
package.SelectSingleNode("package_article/articleid");
if (articleid != null) {
Console.WriteLine("Article: {0}", articleid.InnerText);
}
Console.WriteLine();
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
Thank you for your answer.

However I still can't seem to get the contents under <package_article> to be
retrieved.

How would the line
package.GetElementsByTagName("packageid")[0];

be translated into VB.NET ?
Simply changing the syntax to ...(0) does not seem to work.

Normally I would simply use XSL to output the results, but in this case it's
not possible.
"Martin Honnen" wrote:


Andy wrote:

I have the following example XML:
<data>
<package>
<packageid>123</packageid>
<package_article>
<articleid>article1</articleid>
</package_article>
</package>

<package>
<packageid>456</packageid>
<package_article>
<articleid>article2</articleid>
</package_article>
</package>
</data>

I want to be able to list the following to the client, based on the above xml:
Package: 123
Article: article1
Package: 456
Article: article2


GetElementsByTagName is not only a method of the document itself but of
any element node so you can use that as follows (C# code):

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005070301.xml");
foreach (XmlNode node in xmlDocument.GetElementsByTagName("package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.GetElementsByTagName("packageid")[0];
if (packageid != null) {
Console.WriteLine("Package: {0}", packageid.InnerText);
}
XmlElement articleid = (XmlElement)
package.GetElementsByTagName("articleid")[0];
if (articleid != null) {
Console.WriteLine("Article: {0}", articleid.InnerText);
}
Console.WriteLine();
}

But as .NET implements XPath it is usually easier and more elegant to
solve such tasks with XPath:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005070301.xml");
foreach (XmlNode node in xmlDocument.SelectNodes("/data/package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.SelectSingleNode("packageid");
if (packageid != null) {
Console.WriteLine("Package: {0}", packageid.InnerText);
}
XmlElement articleid = (XmlElement)
package.SelectSingleNode("package_article/articleid");
if (articleid != null) {
Console.WriteLine("Article: {0}", articleid.InnerText);
}
Console.WriteLine();
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


Andy wrote:
How would the line
package.GetElementsByTagName("packageid")[0];

be translated into VB.NET ?


Try with
package.GetElementsByTagName("packageid").ItemOf(0 )

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Con | last post: by
1 post views Thread by xpcer | last post: by
4 posts views Thread by Ouray Viney | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.