1
votes

I am currently having a problem putting the results of a xmlnodelist into a normal list box with the below code.

var xmlDoc = new XmlDocument();
xmlDoc.Load(textBox1.Text);

var node = xmlDoc.SelectNodes("pdml/packet/proto/field[@name='ip.src']/@show");
list.Items.Add(node);

To my understanding the SelectNodes will take all nodes with that XPath name and put it into a list. When I add these to a standard list box I see this displayed:

System.Xml.XPathNodeList

For reference, this specific xml looks as so (it is a portion of a much larger section):

 <pdml>
 <packet>
 <proto>
 <field name="ip.src" showname="Source: 192.168.1.204 (192.168.1.204)" size="4" pos="26" show="192.168.1.204" value="c0a801cc"/>
 </proto>
 </packet>
 </pdml>

How do I convert this into what is contained in the NodeList?

Further help: Also how could I work with the data contained in the NodeList? e.g. can I set it as a unique identifier and assign other node data to it.

Thanks, Tom

3
You want to get all data from each "field" to list? - Sowiarz
The data I want to get out is the show: 192.168.1.204. but from every field in the xml doc with that XPath. - Tom

3 Answers

1
votes

I think this is option, but you have to create list "shows":

XDocument xDoc = XDocument.Load("your xml file");
foreach (var elem in xDoc.Document.Descendants("field[@name='ip.src']"))
{
    shows.Add(elem.Attribute("show").Value);
}
0
votes

node contains collection of XmlAttribute. I believe you meant to add Value of each attribute to list.Items :

foreach (XmlAttribute attribute in node)
{
    list.Items.Add(attribute.Value);
}

or possibly using AddRange() method and LINQ to add all attributes value at once :

list.Items.AddRange(from XmlAttribute attribute in node select attribute.Value);
0
votes

You have to add each item to the list not a collection as one item in the list. As list has only one item of type XmlNodeList it call ToString on this object and it result into System.Xml.XPathNodeList message. You should use list.Items.AddRange if it exist (it is not clear what UI framework you are using) or you should iterate the XmlNodeList collection:

var xmlDoc = new XmlDocument();
xmlDoc.Load(textBox1.Text);

var nodes = xmlDoc.SelectNodes("pdml/packet/proto/field[@name='ip.src']/@show");
foreach(var node in nodes)
    list.Items.Add(node);