473,320 Members | 1,884 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,320 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 33215


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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.