开发者

C#: How do i access data from a deserialized xml-file

开发者 https://www.devze.com 2023-01-23 23:15 出处:网络
I\'ve set up a xml-serialization / deserialization for saving data in my application. I can deserialize non-array elements, but when i deserialize an array it is empty. I have a hunch that the problem

I've set up a xml-serialization / deserialization for saving data in my application. I can deserialize non-array elements, but when i deserialize an array it is empty. I have a hunch that the problem is in the set portion of the property im trying to serialize/deserialize.

First the class i'm trying to serialize:

namespace kineticMold
{
    [Serializable()]
    public class Config
    {
        public Config() { }
        public string ComPort
        {
            get
            {
                return comPort;
            }
            set
            {
                comPort = value;
            }
        }

        [XmlArrayItem("recentFile")]
        public string[] LastOpen
        {

            get
            {
                return lastOpen;
            }
            set
            {

                    ArrayList holderList = new ArrayList();

                    holderList.Add(value);

                    for (int i = 0; i < 4; i++)
                    {

                        holderList.Add(lastOpen[i]);

                    }

                    lastOpen = (string[])lastOpen.ToArray<string>();



            }
        }

        private string comPort;
        private string[] lastOpen = new string[5];


    }
}

The result of the serialization:

<?xml version="1.0" encoding="utf-8" ?> 
<Config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <ComPort>COM12</ComPort> 
    <LastOpen>
       <recentFile>test.xml</recentFile> 
       <recentFile xsi:nil="true" /> 
       <recentFile xsi:nil="true" /> 
       <recentFile xsi:nil="true" /> 
       <recentFile xsi:ni开发者_如何学JAVAl="true" /> 
    </LastOpen>
</Config>

The code for deserialization:

_cf = new Config();
            XmlSerializer ser = new XmlSerializer(typeof(Config));

            if (File.Exists(settings_filepath))
            {
                FileStream fs = new FileStream(@settings_filepath, FileMode.Open);
                _cf = (Config)ser.Deserialize(fs);
                fs.Close();
            }

The code for reading the deserialized data:

 for (int i = 0; i < _cf.LastOpen.Length; i++)
            {

                if (_cf.LastOpen[i] != null)
                {
                    toolStripMenuItem1.DropDownItems.Add(_cf.LastOpen[i]);

                    recentState = true;
                }
            }


You have:

lastOpen = (string[])lastOpen.ToArray<string>();

Do you mean holderList here (on the right hand side)?

It isn't entirely clear which data you want, but you don't currently keep any of the items from value beyond the setter. Also, ArrayList is largely redundant here; it could be a List<string> perhaps?

Of course, even simpler:

private readonly List<string> lastOpen = new List<string>();
[XmlArrayItem("recentFile")]
public List<string> LastOpen {get {return lastOpen;}}

(and let the calling code worry about how many items to keep in there)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号