Click here to Skip to main content
15,887,344 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have some XML data in the form

XML
<GPSettings>
 <ProductCount>3</ProductCount>
 <MainDelimiter>:</MainDelimiter>
 <SubDelimiter>space</SubDelimiter>
<GPProducts>
<GPProduct>
 <ProductName>myProduct1</ProductName>
 <ProductCode>1234</ProductCode>
 <ConfigurationCount>2</ConfigurationCount>
 <Current>1</Current>
<GPConfigurations>
<GPConfiguration>
 <ConfigurationName>myConfigProd1</ConfigurationName>
 <PairCount>7</PairCount>
 <CustomPairCount>1</CustomPairCount>
<GPPairs>
<GPPair>
 <Name>Width1</Name>
 <ResultString>m_Width1</ResultString>
 <DefaultValue>15</DefaultValue>
 <UseDefault>false</UseDefault>
 <IncludeInString>true</IncludeInString>
 <Comment>This is Width 1</Comment>
 <Order>7</Order>
 </GPPair>
<GPair>
 <Name>Width2</Name>
 <ResultString>m_Width2</ResultString>
 <DefaultValue>100</DefaultValue>
 <UseDefault>true</UseDefault>
 <IncludeInString>true</IncludeInString>
 <Comment>This is Width 2</Comment>
 <Order>2</Order>
 </GPPair>



I deserialize into my classes set up as below:

C#
[Serializable()]
    [XmlRoot("Settings")]
    public class Settings
    {
        [XmlElement("ProductCount")]
        public int ProductCount{ get; set; }
        [XmlElement("MainDelimiter")]
        public string MainDelimiter { get; set; }
        [XmlElement("SubDelimiter")]
        public string SubDelimiter { get; set; }

        [XmlArray("GPProducts")]
        [XmlArrayItem("GPProduct", typeof(GPProduct))]
        public GPProduct[] GPProduct { get; set; }



C#
[Serializable()]
    public class GPProduct
    {
        [XmlElement("ProductName")]
        public string ProductName { get; set; }
        [XmlElement("ProductCode")]
        public string ProductCode { get; set; }
        [XmlElement("ConfigurationCount")]
        public int ConfigurationCount { get; set; }
        [XmlElement("Current")]
        public int Current { get; set; }

        [XmlArray("GPConfigurations")]
        [XmlArrayItem("GPConfiguration", typeof(GPConfiguration))]
        public GPConfiguration[] GPConfiguration { get; set; }



C#
[Serializable()]
    public class GPConfiguration
    {
        [XmlElement("ConfigurationName")]
        public string ConfigurationName { get; set; }
        [XmlElement("PairCount")]
        public int PairCount { get; set; }
        [XmlElement("CustomPairCount")]
        public int CustomPairCount { get; set; }

        [XmlArray("GPPairs")]
        [XmlArrayItem("GPPair", typeof(GPPair))]
        public GPPair[] GPPair { get; set; }



C#
[Serializable]
    public class GPPair : IComparable
    {
        [XmlElement("Name")]
        public string Name { get; set; }
        [XmlElement("ResultString")]
        public string ResultString { get; set; }
        [XmlElement("DefaultValue")]
        public string DefaultValue { get; set; }
        [XmlElement("UseDefault")]
        public bool UseDefault { get; set; }
        [XmlElement("IncludeInString")]
        public bool IncludeInString { get; set; }
        [XmlElement("Comment")]
        public string Comment { get; set; }
        [XmlElement("Order")]
        public int Order { get; set; }



Deserialization with:

C#
string path = @"C:\test.xml";
            XmlSerializer serializer = new XmlSerializer(typeof(GPSettings));
            StreamReader reader = new StreamReader(path);
            GPSettings = (GPSettings)serializer.Deserialize(reader);
            reader.Close();



The main form contains two combo boxes which allow for selection of the GPProduct and the GPConfigurations, and then the GPPair values are all displayed in a datagridview.

I want to be able to add a row to this datagridview and have the underlying object updated for serialization later. I can find no good way to get this done, any ideas?

Thanks
Posted

You want to dynamically add a property to your class based on user input and have it available for serialization? Correct?

If so then the only way to accomplish that I can think of is using CodeDOM to dynamically construct the class.http://msdn.microsoft.com/en-us/library/y2k85ax6.aspx[^]
 
Share this answer
 
I dont need to add a new property no, only a new GPPair - a row of data in the GPPair class.
 
Share this answer
 
Comments
[no name] 17-Oct-11 11:49am    
Use the Have a Question or Comment? link, don't add a new solution

GPPair is an Array. Add as many objects to it as you like. What is the problem?
Member 8170464 17-Oct-11 11:55am    
Perhaps I'm missing something then, I can find no method to add when I use the syntax

GPSettings.GPProduct[0].GPConfiguration[0].GPPair ....

I was hoping for an .Add method there. I need to create a new element in that GPPair array and I can't find a way to do it. GPPair[3] = is outside the bounds of the array for example. I need to resize that array that was create dynamically on deserialization.
BobJanova 17-Oct-11 12:20pm    
Can you declare that field as a List<GPPair>? I'm not sure if the serialiser will work with that. If not, I suggest you create a property which on first use creates a list from the array, and on request (you'll have to call this method when you want to save, unless there's a hook in ISerializable you can override for it) resize and repopulate the array with what's now in the list.
[no name] 17-Oct-11 12:54pm    
There is no Add method on the Array class, they are fixed length, you may be confusing it with an IList.
Member 8170464 18-Oct-11 4:14am    
So how then would I add a new element to the ....GPPair array?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900