tags:

views:

25

answers:

0

Hi to all,

I am reading up more on XAML and I have created an XML file and want to load it. It all works but when I use the ContentProperty attribute and more importantly I remove the Company.Employees tags from the XML file, I get the error "cannot add to Company" when loading the xml file.

The code is :

<?xml version="1.0" encoding="utf-8" ?>
 <Company Name="BBC" xmlns="clr-namespace:XamlLoading;assembly=XamlLoading">
  <Company.Owner> 
    <Person Name="John" Age="49"/>
  </Company.Owner>

  <!--<Company.Employees>
    <Person Name="Dave" Age="66" />
     <Person Name="Paul" Age="45"/>
  </Company.Employees>-->

 <Person Name="Dave" Age="66" />
 <Person Name="Paul" Age="45"/>
</Company>

[ContentProperty("Employees")]
public class Company 
{
    public Company()
    {
        Employees = new List<Person>();
    }

    public Person Owner { get; set; }

    public string Name { get; set; }
    public List<Person> Employees { get; set; }
}


    static void Main(string[] args)
    {
        using (FileStream fs = File.OpenRead(@"..\..\company.xml"))
        {

            Company c = (Company)XamlReader.Load(fs); * ERROR HERE

            Console.WriteLine(c.Name);
            Console.WriteLine(c.Owner);

            foreach (var item in c.Employees)
            {
                Console.WriteLine("{0} : ", item);
            }

            Console.ReadLine();
        }
    }