Reputation: 139
I would like to achieve the following :
<?xml version="1.0" encoding="utf-8"?>
<StatisticsFunctionsSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StatisticsFunctions>
<Visibility>false</Visibility>
</StatisticsFunctions>
</StatisticsFunctionsSetting>
using the following bool property
[XmlArray("StatisticsFunctions")]
[XmlArrayItem("Visibility")]
public bool IsShowChecked
{
get
{
return this.isShowChecked;
}
set
{
this.isShowChecked = value;
this.OnPropertyChanged("IsShowChecked");
}
}
It is crashing on XmlSerializer.Deserialize()
. Does the property have to be an array and not bool? I would like to keep the boolean property, so please advice the XML attribute to use.
Upvotes: 0
Views: 52
Reputation: 34421
Try this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"C:\temp\test.xml";
static void Main(string[] args)
{
StatisticsFunctionsSetting settings = new StatisticsFunctionsSetting(){
statisticsFunctions = new List<StatisticsFunctions>(){
new StatisticsFunctions(){
visibility = new List<bool>(){true,true, false}
}
}
};
XmlSerializer serializer = new XmlSerializer(typeof(StatisticsFunctionsSetting));
StreamWriter writer = new StreamWriter(FILENAME);
XmlSerializerNamespaces _ns = new XmlSerializerNamespaces();
_ns.Add("", "");
serializer.Serialize(writer, settings, _ns);
writer.Flush();
writer.Close();
writer.Dispose();
XmlSerializer xs = new XmlSerializer(typeof(StatisticsFunctionsSetting));
XmlTextReader reader = new XmlTextReader(FILENAME);
StatisticsFunctionsSetting newSettings = (StatisticsFunctionsSetting)xs.Deserialize(reader);
}
}
[XmlRoot("StatisticsFunctionsSetting")]
public class StatisticsFunctionsSetting
{
[XmlElement("StatisticsFunctions")]
public List<StatisticsFunctions> statisticsFunctions {get;set;}
}
[XmlRoot("StatisticsFunctions")]
public class StatisticsFunctions
{
[XmlElement("Visibility")]
public List<Boolean> visibility { get; set; }
}
}
Upvotes: 1
Reputation: 2522
From MSDN: You can apply the XmlArrayAttribute to a public field or read/write property that returns an array of objects. You can also apply it to collections and fields that return an ArrayList or any field that returns an object that implements the IEnumerable interface.
Use an array of bool or manual serialize/deserialize.
Upvotes: 1