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. XML Formatted String and Collection with dynamic value types

XML Formatted String and Collection with dynamic value types

Scheduled Pinned Locked Moved C#
csharpxmltutorialquestion
5 Posts 4 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.
  • B Offline
    B Offline
    budidharma
    wrote on last edited by
    #1

    I have an XML formatted string that my program receives from any number of other programs. It may contain: < value="name" type="string" >Aristotle< /value > (Without spaces around value tags) I need to have a collection which will dynamically create those variables inside itself, but I'm not sure how to do this. Something like: public class XMLParser { string m_RawXML; CollectionWithMultipleTypes m_Values = new CollectionWithMultipleTypes() // IS THERE A COLLECTION TYPE BUILT FOR THIS PURPOSE? public XMLParser(string RawXML) { ParseXMLString(string RawXML); } public void ParseXMLString(string RawXML) { string[] ValueStringsArray = GetValueStrings(string RawXML); foreach(string valueString in ValueStringsArray) { try { // Parse string to create new type with name and store data in it mValues.Add(newVal); } catch(Exception err) { MessageBox.Show(err.ToString()); } } } } So, my questions are: Is there a datatype in C# that can store mulitple types of data in it? There aren't built in XML functions for recreating data types from a string, are there? Is this a viable approach? If it is, does anyone have any code that might simplify my life? As always, Thanks! -- modified at 15:22 Thursday 3rd November, 2005

    W J 2 Replies Last reply
    0
    • B budidharma

      I have an XML formatted string that my program receives from any number of other programs. It may contain: < value="name" type="string" >Aristotle< /value > (Without spaces around value tags) I need to have a collection which will dynamically create those variables inside itself, but I'm not sure how to do this. Something like: public class XMLParser { string m_RawXML; CollectionWithMultipleTypes m_Values = new CollectionWithMultipleTypes() // IS THERE A COLLECTION TYPE BUILT FOR THIS PURPOSE? public XMLParser(string RawXML) { ParseXMLString(string RawXML); } public void ParseXMLString(string RawXML) { string[] ValueStringsArray = GetValueStrings(string RawXML); foreach(string valueString in ValueStringsArray) { try { // Parse string to create new type with name and store data in it mValues.Add(newVal); } catch(Exception err) { MessageBox.Show(err.ToString()); } } } } So, my questions are: Is there a datatype in C# that can store mulitple types of data in it? There aren't built in XML functions for recreating data types from a string, are there? Is this a viable approach? If it is, does anyone have any code that might simplify my life? As always, Thanks! -- modified at 15:22 Thursday 3rd November, 2005

      W Offline
      W Offline
      Wjousts
      wrote on last edited by
      #2

      budidharma wrote:

      So, my questions are: Is there a datatype in C# that can store mulitple types of data in it?

      Since all classes are derived from the Object class any collection that can hold objects (e.g. an Array or ArrayList) is capable of holding objects of any class. For recreating objects from XML I think you'll want to look in to the Reflection classes. You'd need to extract the type attribute from your XML string and use reflection to try and create the object of that class. Maybe it would also be possible to use serialization to recreate your objects?

      B 1 Reply Last reply
      0
      • W Wjousts

        budidharma wrote:

        So, my questions are: Is there a datatype in C# that can store mulitple types of data in it?

        Since all classes are derived from the Object class any collection that can hold objects (e.g. an Array or ArrayList) is capable of holding objects of any class. For recreating objects from XML I think you'll want to look in to the Reflection classes. You'd need to extract the type attribute from your XML string and use reflection to try and create the object of that class. Maybe it would also be possible to use serialization to recreate your objects?

        B Offline
        B Offline
        budidharma
        wrote on last edited by
        #3

        Is there a build in XMLRead type command that will retrieve name value pairs from an xml formatted string?

        S 1 Reply Last reply
        0
        • B budidharma

          Is there a build in XMLRead type command that will retrieve name value pairs from an xml formatted string?

          S Offline
          S Offline
          S Senthil Kumar
          wrote on last edited by
          #4

          Look for the System.Xml.XmlDocument[^] class and related classes. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          1 Reply Last reply
          0
          • B budidharma

            I have an XML formatted string that my program receives from any number of other programs. It may contain: < value="name" type="string" >Aristotle< /value > (Without spaces around value tags) I need to have a collection which will dynamically create those variables inside itself, but I'm not sure how to do this. Something like: public class XMLParser { string m_RawXML; CollectionWithMultipleTypes m_Values = new CollectionWithMultipleTypes() // IS THERE A COLLECTION TYPE BUILT FOR THIS PURPOSE? public XMLParser(string RawXML) { ParseXMLString(string RawXML); } public void ParseXMLString(string RawXML) { string[] ValueStringsArray = GetValueStrings(string RawXML); foreach(string valueString in ValueStringsArray) { try { // Parse string to create new type with name and store data in it mValues.Add(newVal); } catch(Exception err) { MessageBox.Show(err.ToString()); } } } } So, my questions are: Is there a datatype in C# that can store mulitple types of data in it? There aren't built in XML functions for recreating data types from a string, are there? Is this a viable approach? If it is, does anyone have any code that might simplify my life? As always, Thanks! -- modified at 15:22 Thursday 3rd November, 2005

            J Offline
            J Offline
            Jon Rista
            wrote on last edited by
            #5

            I would check out the XmlTextReader class. Its a very low-profile, rapid, and generally forward-specific class that can blast through huge xml files in short order. Its VERY easy to use, and should solve your problem in a heartbeat. You can likewise use XmlTextWriter to reserialize your collection to similar xml, too. :)

            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