Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XmlNodes in a ListBox and junk...

XmlNodes in a ListBox and junk...

Scheduled Pinned Locked Moved C#
xmlhelpquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    Hi. I'm adding a bunch of XmlNodes into a ListBox control. Now, when I look at the list box, I see a bunch of System.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 the XmlNode class, so I can't use DisplayMember. In order to get the text, I need to run a XmlNode.SelectSingleNode().InnerText. So far, I haven't had any luck putting anything like that into DisplayMember. 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? /\ |_ E X E GG

    J D L 3 Replies Last reply
    0
    • E eggie5

      Hi. I'm adding a bunch of XmlNodes into a ListBox control. Now, when I look at the list box, I see a bunch of System.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 the XmlNode class, so I can't use DisplayMember. In order to get the text, I need to run a XmlNode.SelectSingleNode().InnerText. So far, I haven't had any luck putting anything like that into DisplayMember. 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? /\ |_ E X E GG

      J Offline
      J Offline
      John Arlen1
      wrote on last edited by
      #2

      For 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) );

      1 Reply Last reply
      0
      • E eggie5

        Hi. I'm adding a bunch of XmlNodes into a ListBox control. Now, when I look at the list box, I see a bunch of System.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 the XmlNode class, so I can't use DisplayMember. In order to get the text, I need to run a XmlNode.SelectSingleNode().InnerText. So far, I haven't had any luck putting anything like that into DisplayMember. 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? /\ |_ E X E GG

        D Offline
        D Offline
        DavidNohejl
        wrote on last edited by
        #3

        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

        E 1 Reply Last reply
        0
        • D DavidNohejl

          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

          E Offline
          E Offline
          eggie5
          wrote on last edited by
          #4

          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

          S 1 Reply Last reply
          0
          • E eggie5

            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

            S Offline
            S Offline
            Sebrell
            wrote on last edited by
            #5

            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.

            E 1 Reply Last reply
            0
            • S Sebrell

              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.

              E Offline
              E Offline
              eggie5
              wrote on last edited by
              #6

              that went way over my head. thanks for your time. /\ |_ E X E GG

              1 Reply Last reply
              0
              • E eggie5

                Hi. I'm adding a bunch of XmlNodes into a ListBox control. Now, when I look at the list box, I see a bunch of System.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 the XmlNode class, so I can't use DisplayMember. In order to get the text, I need to run a XmlNode.SelectSingleNode().InnerText. So far, I haven't had any luck putting anything like that into DisplayMember. 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? /\ |_ E X E GG

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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!

                A 1 Reply Last reply
                0
                • L Lost User

                  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!

                  A Offline
                  A Offline
                  Anonymous
                  wrote on last edited by
                  #8

                  Sorry, Xml went wrong. The xml i used in that LoadXml call was foobar

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups