473,396 Members | 2,052 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,396 software developers and data experts.

Need some basic help

AGB
Hi all,

I have the following simple XML file. I would like to load all the
values in the Name nodes into a drop-down list box:

<?xml version="1.0" encoding="utf-8" ?>
<OutlineCodeNames>
<OutlineCodeName>
<UID>23423523</UID>
<Name>Contract</Name>
<K234432344/>
</OutlineCodeName>
<OutlineCodeName>
<UID>16345345</UID>
<Name>Project Charge Number</Name>
<K66634535/>
</OutlineCodeName>
</OutlineCodeNames>

Here is the code I am using:

private void PopulateComboBox()
{
XmlDocument doc = new XmlDocument();
XmlNode rootNode;
XmlNodeList methodNodes;

doc.Load(Application.StartupPath + @"\PDSTest.xml");
rootNode = doc.DocumentElement;
methodNodes = rootNode.SelectNodes("OutlineCodeName");
cmbMethod.BeginUpdate();
foreach (XmlNode methodNode in methodNodes)
{
cmbMethod.Items.Add(methodNode.FirstChild.NextSibl ing.Value);
}
cmbMethod.EndUpdate();

cmbMethod.SelectedIndex = 0;
}
I am getting nothing back. Any help?

Nov 12 '05 #1
2 1383
1. Define your rootNode as an XmlElement insted of an XmlNode:
XmlElement rootNode;

2. And add to the combobox the InnerXml value:
cmbMethod.Items.Add(methodNode.FirstChild.NextSibl ing.InnerXml);

Hope this helped.

AGB wrote:
Hi all,

I have the following simple XML file. I would like to load all the
values in the Name nodes into a drop-down list box:

<?xml version="1.0" encoding="utf-8" ?>
<OutlineCodeNames>
<OutlineCodeName>
<UID>23423523</UID>
<Name>Contract</Name>
<K234432344/>
</OutlineCodeName>
<OutlineCodeName>
<UID>16345345</UID>
<Name>Project Charge Number</Name>
<K66634535/>
</OutlineCodeName>
</OutlineCodeNames>

Here is the code I am using:

private void PopulateComboBox()
{
XmlDocument doc = new XmlDocument();
XmlNode rootNode;
XmlNodeList methodNodes;

doc.Load(Application.StartupPath + @"\PDSTest.xml");
rootNode = doc.DocumentElement;
methodNodes = rootNode.SelectNodes("OutlineCodeName");
cmbMethod.BeginUpdate();
foreach (XmlNode methodNode in methodNodes)
{
cmbMethod.Items.Add(methodNode.FirstChild.NextSibl ing.Value);
}
cmbMethod.EndUpdate();

cmbMethod.SelectedIndex = 0;
}
I am getting nothing back. Any help?


Nov 12 '05 #2
"AGB" <ag*******@hotmail.com> wrote in message news:11**********************@z14g2000cwz.googlegr oups.com...
I would like to load all the values in the Name nodes into a drop-down list box: : : <OutlineCodeNames>
<OutlineCodeName>
<UID>23423523</UID>
<Name>Contract</Name> : : methodNodes = rootNode.SelectNodes("OutlineCodeName");


SelectNodes( ) takes a string argument that needs to be an XPath
expression. You receive nothing back because when it processes
the XPath, ''OutlineCodeName'', it compares it to the document
element's name only. The document element's name had a trailing
-s, ''OutlineCodeNames'', which doesn't match.

If it's the Name grandchild element you want the inner text of, then
you need to write an XPath expression with what are called "location
steps" to reach the Name node. XPath is hierarchical, like your PC's
file system.

Start at the root, /OutlineCodeNames.

From all of the immediate children of OutlineCodeNames, you want
OutlineCodeName elements (and if there were elements with other
names they would be filtered out by this 'step'). This gives you the
XPath, /OutlineCodeNames/OutlineCodeName.

Taking another look at all of the immediate children of OutlineCodeName
(UID, Name, etc.), the element name you want to take in the next step
is Name. This gives you the XPath expression:

/OutlineCodeNames/OutlineCodeName/Name

If you plug that string into what you've passing to SelectNodes( )
you should receive an XmlNodeList with one XmlNode for each
<Name> element.

Then take each <Name> elements' FirstChild.Value and Add( )
the names to your DropDown. With the example XML document
this should give you items for ''Contract'' and ''Project Charge
Number''.

Alternately, you could also write the XPath expression this way,

/OutlineCodeNames/OutlineCodeName/Name/text( )

which will return you an XmlNodeList containing XmlText nodes,
from which you can directly take the Value from.

Further, if you're interested in all elements named <Name> you
can get all elements named <Name> no matter where they are
in the document using the descendant-or-self axis, //.

//Name/text( )

The drawback is its not as efficient, and it's less specific about
which <Name> elements it returns (although you can get more
specific, such as //OutlineCodeName/Name/text( ) to ensure it
only returns the text of <Name> elements that are direct children
of <OutlineCodeName> -- and get none that are the direct
children of <DraftEditor>.)
Derek Harmon
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: AK | last post by:
I don't want any part of the previous discussion on Visual Basic versus Visual Basic.Net. My query is about using Visual Basic for Applications; and whether it is better to use Visual Basic 6 or...
4
by: Steve Richfie1d | last post by:
I wrote my first BASIC compiler when Bill Gates was going to Lakeside School, and am believed to be the original inventor of the ON ERROR statement. Now, my son wants to learn Visual Basic, but...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
1
by: Tom Rahav | last post by:
Hello all! I develop application in Visual Basic .NET and ORACLE database. My question is how do I "send" script file to the database using visual basic .net. Other words, is there any way to...
13
by: usenet | last post by:
How and where can one find out about the basics of VB/Access2003 syntax? I am a died in the wool C/C++/Java Linux/Unix programmer and I am finding it difficult to understand the program format...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
0
CLAW
by: CLAW | last post by:
Hi, I'm new with Visual Basic. I've started a Web Browser project in Visual Basic 2005 Express Edition, but being new I really need some help: 1) When the user presses a button, I want to execute an...
51
by: Steven Spits | last post by:
Hi, Plannig to buy Vista, but not sure what version to get. I do VS.NET development, mostly ASP.NET. Can IIS be installed on Vista home premium? Or do I need business or ultimate? Steven
4
by: ooroboo | last post by:
i wont a text file to load into a rich text on box on load , then save when the form closes. my problem has been creating the text file , i am very new for visual basic and could use some help this...
3
by: shimul | last post by:
Hi All, Have query where I get the result like (except "-------------------",put it to separate the two columns) NUMBER ---------------TYPE DESCRIPTION 17 --------------------------Basic 17...
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...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.