473,809 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to retrieve XML sublevel, using GetElementsByTa gName

Hello,

I have the following example XML:
<data>
<package>
<packageid>12 3</packageid>
<package_articl e>
<articleid>arti cle1</articleid>
</package_article >
</package>

<package>
<packageid>45 6</packageid>
<package_articl e>
<articleid>arti cle2</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.GetEleme ntsByTagName("p ackage")
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 33292


Andy wrote:

I have the following example XML:
<data>
<package>
<packageid>12 3</packageid>
<package_articl e>
<articleid>arti cle1</articleid>
</package_article >
</package>

<package>
<packageid>45 6</packageid>
<package_articl e>
<articleid>arti cle2</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


GetElementsByTa gName 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.Loa d(@"test2005070 301.xml");
foreach (XmlNode node in xmlDocument.Get ElementsByTagNa me("package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.GetElem entsByTagName(" packageid")[0];
if (packageid != null) {
Console.WriteLi ne("Package: {0}", packageid.Inner Text);
}
XmlElement articleid = (XmlElement)
package.GetElem entsByTagName(" articleid")[0];
if (articleid != null) {
Console.WriteLi ne("Article: {0}", articleid.Inner Text);
}
Console.WriteLi ne();
}

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

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"test2005070 301.xml");
foreach (XmlNode node in xmlDocument.Sel ectNodes("/data/package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.SelectS ingleNode("pack ageid");
if (packageid != null) {
Console.WriteLi ne("Package: {0}", packageid.Inner Text);
}
XmlElement articleid = (XmlElement)
package.SelectS ingleNode("pack age_article/articleid");
if (articleid != null) {
Console.WriteLi ne("Article: {0}", articleid.Inner Text);
}
Console.WriteLi ne();
}
--

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_articl e> to be
retrieved.

How would the line
package.GetElem entsByTagName(" 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>12 3</packageid>
<package_articl e>
<articleid>arti cle1</articleid>
</package_article >
</package>

<package>
<packageid>45 6</packageid>
<package_articl e>
<articleid>arti cle2</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


GetElementsByTa gName 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.Loa d(@"test2005070 301.xml");
foreach (XmlNode node in xmlDocument.Get ElementsByTagNa me("package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.GetElem entsByTagName(" packageid")[0];
if (packageid != null) {
Console.WriteLi ne("Package: {0}", packageid.Inner Text);
}
XmlElement articleid = (XmlElement)
package.GetElem entsByTagName(" articleid")[0];
if (articleid != null) {
Console.WriteLi ne("Article: {0}", articleid.Inner Text);
}
Console.WriteLi ne();
}

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

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa d(@"test2005070 301.xml");
foreach (XmlNode node in xmlDocument.Sel ectNodes("/data/package")) {
XmlElement package = node as XmlElement;
XmlElement packageid = (XmlElement)
package.SelectS ingleNode("pack ageid");
if (packageid != null) {
Console.WriteLi ne("Package: {0}", packageid.Inner Text);
}
XmlElement articleid = (XmlElement)
package.SelectS ingleNode("pack age_article/articleid");
if (articleid != null) {
Console.WriteLi ne("Article: {0}", articleid.Inner Text);
}
Console.WriteLi ne();
}
--

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

Nov 12 '05 #3


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

be translated into VB.NET ?


Try with
package.GetElem entsByTagName(" packageid").Ite mOf(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
1648
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. <% set popctrl = Server.CreateObject("adodb.connection") popctrl.Provider = "ExOLEDB.DataSource" popctrl.Open "mail.company.com",user@mail.com,"passwd" %>
1
3148
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 `monitoring_contract`.`sp_get_jns_contract_get_id`$$
1
3455
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, sum(nPassengers) Planned from tbl_BusRequest where iReqTime = '1' and iDeptCode = '1' and cReqType = 'I' group by iDeptCode and
1
1920
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 specify just 'Details'
9
17582
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
2141
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 effective at this point. my js script code below is obviously and the current problem im having is figuring out how to build the array to store the information of each of the hidden divs (class = "headline"). // Rotating headlines function...
2
4431
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
6982
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
1032
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 version="1.0" encoding="UTF-8"?> <WdprAccountProfileResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://Broadridge.com/Ascendis/Services/PhaseIV/Entities/"> <Details i:nil="true" /> <RawData> <ADP_MESSAGE...
0
10633
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10375
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3860
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.