Groups | Blog | Home
all groups > dotnet xml > january 2005 >

dotnet xml : Need some basic help



AGB
1/7/2005 12:53:28 PM
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.NextSibling.Value);
}
cmbMethod.EndUpdate();

cmbMethod.SelectedIndex = 0;
}


I am getting nothing back. Any help?
EllenSand NO[at]SPAM gmail.com
1/7/2005 1:29:44 PM
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.NextSibling.InnerXml);

Hope this helped.

[quoted text, click to view]
Derek Harmon
1/8/2005 2:12:20 AM
[quoted text, click to view]

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

AddThis Social Bookmark Button