473,372 Members | 1,113 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,372 software developers and data experts.

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 33223


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Con | last post by:
I am trying to access a remote email host via ASP to retrieve a list of messages for the mail account, I have tried the following code to no avail, any suggestions or pointers would be appreciated....
1
by: xpcer | last post by:
hi friends, i have an problem. i use php 4 and mysql 5.0 i was create an store procedure in mysql like this, DELIMITER $$; DROP PROCEDURE IF EXISTS...
1
by: intscript | last post by:
Hi everyone Is is possible to retrieve data using multiple select. Here is my SQL code: select sum(nPassengers) Planned, Actual, (sum(Planned) - Actual) Variance from ( select iDeptCode,...
1
by: luthriaajay | last post by:
I am trying to go to a subchild node of an XML document. by saying Document.getElementsByTagName("Details/Market"); but its not allowing me to use sub child nodes. It works all right if I...
9
by: hiteshverma | last post by:
Hello friends, I am a new member to this community. I have a basic problem. I have two tables. 1) books having fields id, name, author, publication
10
by: scoobydoo9749 | last post by:
hey everyone, so ive been going around the internet all day trying to help guide me in creating a div that rotates content every 4 seconds using ajax and ive decided a forum post might be the most...
2
by: waruni | last post by:
Hi.. In my project i need to retrieve data from a table using javascript.I'm new to this subject.So.. if any one have the answer plz let me know.
4
by: Ouray Viney | last post by:
Xml <ib>8.4.27.5</ib> python from xml.dom import minidom xmldoc = minidom.parse('C:\TestProfile.xml') xmldoc
0
by: kavijaya | last post by:
Error message: "Unable to retrieve nodes using SelectSingleNode." Can anyone tell me correct way of implementing this? The following code always returns null: (XML I am working with) <?xml...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.