XmlNodes in a ListBox and junk...
-
Hi. I'm adding a bunch of
XmlNodes
into a ListBox control. Now, when I look at the list box, I see a bunch ofSystem.Xml.XmlElements
(which is expected). I would like to be able to display in the ListBox some specified text, but the problem is, the text is not just a public property in theXmlNode
class, so I can't useDisplayMember
. In order to get the text, I need to run aXmlNode.SelectSingleNode().InnerText
. So far, I haven't had any luck putting anything like that intoDisplayMember
. Is it possible to get the inner text of a single node from an object in myListBox
and have it displayed instead of System.Xml.XmlElement? Or am I going to have to wrap it in a class or something? /\ |_ E X E GG -
Hi. I'm adding a bunch of
XmlNodes
into a ListBox control. Now, when I look at the list box, I see a bunch ofSystem.Xml.XmlElements
(which is expected). I would like to be able to display in the ListBox some specified text, but the problem is, the text is not just a public property in theXmlNode
class, so I can't useDisplayMember
. In order to get the text, I need to run aXmlNode.SelectSingleNode().InnerText
. So far, I haven't had any luck putting anything like that intoDisplayMember
. Is it possible to get the inner text of a single node from an object in myListBox
and have it displayed instead of System.Xml.XmlElement? Or am I going to have to wrap it in a class or something? /\ |_ E X E GGFor any item added to a ListBox, the text returned by .ToString() is displayed. The simplest solution is to create a wrapper class that overrides ToString() public class NodeItem { private XmlNode m_node; public XmlNode Node {...} public NodeItem(XmlNode node) { m_node = node; } public override string ToString() { return m_node.InnerText; } } myListBox.Items.Add( new NodeItem(myNode) );
-
Hi. I'm adding a bunch of
XmlNodes
into a ListBox control. Now, when I look at the list box, I see a bunch ofSystem.Xml.XmlElements
(which is expected). I would like to be able to display in the ListBox some specified text, but the problem is, the text is not just a public property in theXmlNode
class, so I can't useDisplayMember
. In order to get the text, I need to run aXmlNode.SelectSingleNode().InnerText
. So far, I haven't had any luck putting anything like that intoDisplayMember
. Is it possible to get the inner text of a single node from an object in myListBox
and have it displayed instead of System.Xml.XmlElement? Or am I going to have to wrap it in a class or something? /\ |_ E X E GGeggie5 wrote: Is it possible to get the inner text of a single node from an object in my ListBox and have it displayed instead of System.Xml.XmlElement? Or am I going to have to wrap it in a class or something? Is that text acessible via property? If no, you MUST wrapp it. ( and/or override
ToString()
). Docs say it pretty clearly. "A String specifying the name of a property of the object specified by the DataSource property. The default is an empty string ("")." (MSDN[^]) David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
eggie5 wrote: Is it possible to get the inner text of a single node from an object in my ListBox and have it displayed instead of System.Xml.XmlElement? Or am I going to have to wrap it in a class or something? Is that text acessible via property? If no, you MUST wrapp it. ( and/or override
ToString()
). Docs say it pretty clearly. "A String specifying the name of a property of the object specified by the DataSource property. The default is an empty string ("")." (MSDN[^]) David Never forget: "Stay kul and happy" (I.A.)
David's thoughts / dnhsoftware.org / MyHTMLTidy -
dnh wrote: "A String specifying the name of a property of the object specified by the DataSource property. The default is an empty string ("")." (MSDN[^]) Yeah, but the code was so clean looking... /\ |_ E X E GG
This is not a simple solution like the above, but it is easy to do in Visual Studio: get the schema for your documents, create a typed DataSet from the schema, use a dataset instance in your form, and set the appropriate table/column for the ListBox.DataSource and ListBox.DataMember properties.
Just a thought.
-
This is not a simple solution like the above, but it is easy to do in Visual Studio: get the schema for your documents, create a typed DataSet from the schema, use a dataset instance in your form, and set the appropriate table/column for the ListBox.DataSource and ListBox.DataMember properties.
Just a thought.
-
Hi. I'm adding a bunch of
XmlNodes
into a ListBox control. Now, when I look at the list box, I see a bunch ofSystem.Xml.XmlElements
(which is expected). I would like to be able to display in the ListBox some specified text, but the problem is, the text is not just a public property in theXmlNode
class, so I can't useDisplayMember
. In order to get the text, I need to run aXmlNode.SelectSingleNode().InnerText
. So far, I haven't had any luck putting anything like that intoDisplayMember
. Is it possible to get the inner text of a single node from an object in myListBox
and have it displayed instead of System.Xml.XmlElement? Or am I going to have to wrap it in a class or something? /\ |_ E X E GGThere is 2 ways to achive this... one is to write a wrapper around an XmlNode, the other (and slightly preferable IMHO) is to write a little ListItem class.
class XmlNodeInnerTextListItem { private string data; public XmlNodeInnerTextListItem(string xpath, XmlNode node) { this.data = node.SelectSingleNode(xpath).InnerText; } public override string ToString() { return this.data; } }
I tested with this code and it worked fine:XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml("foobar"); XmlNode node = xmldoc.DocumentElement.FirstChild; this.listBox1.Items.Add(new XmlNodeInnerTextListItem("data",node)); this.listBox1.Items.Add(new XmlNodeInnerTextListItem("data",node.NextSibling));
Listbox had "foo" and "bar" in the list! -
There is 2 ways to achive this... one is to write a wrapper around an XmlNode, the other (and slightly preferable IMHO) is to write a little ListItem class.
class XmlNodeInnerTextListItem { private string data; public XmlNodeInnerTextListItem(string xpath, XmlNode node) { this.data = node.SelectSingleNode(xpath).InnerText; } public override string ToString() { return this.data; } }
I tested with this code and it worked fine:XmlDocument xmldoc = new XmlDocument(); xmldoc.LoadXml("foobar"); XmlNode node = xmldoc.DocumentElement.FirstChild; this.listBox1.Items.Add(new XmlNodeInnerTextListItem("data",node)); this.listBox1.Items.Add(new XmlNodeInnerTextListItem("data",node.NextSibling));
Listbox had "foo" and "bar" in the list!