绑定和修改XML反序列化对象 [英] Binding and modifying an XML deserialised object

查看:94
本文介绍了绑定和修改XML反序列化对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些XML数据,形式为

I have some XML data in the form

<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:

[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; }







[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; }







[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; }







[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:

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




主窗体包含两个组合框,用于选择GPProduct和GPConfigurations,然后GPPair值都显示在datagridview中.

我希望能够在此datagridview中添加一行,并更新基础对象以便以后进行序列化.我找不到解决这个问题的好方法,有什么主意吗?

谢谢




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

推荐答案

您要基于用户输入向类动态添加属性,并将其用于序列化吗?正确吗?

如果是这样,那么我能想到的唯一实现方法是使用CodeDOM动态构造该类. http: //msdn.microsoft.com/zh-CN/library/y2k85ax6.aspx [
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[^]


我不需要添加新属性no,只需添加新GPPair-GPPair类中的一行数据.
I dont need to add a new property no, only a new GPPair - a row of data in the GPPair class.


这篇关于绑定和修改XML反序列化对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆