JD.
JD.

Reputation: 15551

XAML loading error using contentProperty

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();
        }
    }

Upvotes: 2

Views: 202

Answers (1)

Josh G
Josh G

Reputation: 14256

I would suggest that you update to the latest version of .NET. Your code works great for me.

This is a very interesting case study for XAML. I have never seen it used this way before. Typically XAML is used like HTML markup for declaratively defining a user interface. Blend is a WYSIWYG editor for creating user interfaces by generating XAML.

You have demonstrated some interesting potential for realizing data from xml using the XamlReader.

Upvotes: 1

Related Questions