XML Formatted String and Collection with dynamic value types
-
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 -
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, 2005budidharma 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?
-
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?
Is there a build in XMLRead type command that will retrieve name value pairs from an xml formatted string?
-
Is there a build in XMLRead type command that will retrieve name value pairs from an xml formatted string?
Look for the System.Xml.XmlDocument[^] class and related classes. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
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, 2005I 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. :)