Search code examples
c#xmlasp.net-mvcdynamic-data

How to edit/view a dynamic object in Asp.Net MVC 2 or 3?


I want to used objects that are entirely made of xml, and i want to make a view framework the would allow me to edit / view this object in a asp.net mvc view.

Do you have an idea on how could I accomplish this?

Any idea is good.

Thank you

Edit 1: Example of xml, but this is basic, i want to represent any kind of data in this xml, including base64 pictures

<?xml version="1.0" encoding="UTF-8"?>
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">  
     <name xsi:nil="true"></name>
     <description>bqddd</description>
</product>

Edit 2: I want to store as xml properties for objects, each object with different properties. And when I edit an object i want to be able to show a different type of interface dinamically for each type of object i have as xml.

Edit 3: I want also to be able to change the view on the fly without the need to recompile, if posible.


Solution

  • So off the bat I just wrote some code unsure of what you want.

    Based on your xml you could just add a ViewModel like:

    class Product
    {
     public string Name..
     public string Description..
    }
    

    But then you said something about being dynamic and there is something interesting you can do with the ExpandoObject class.

    Check this code:

    void Main()
    {
        XmlDynamicModel x = new XmlDynamicModel(@"path/myobject.xml");
        //you're element should be <description>value</description>
        //I would rather capitalize the first letter **Description
        Console.WriteLine(x.TheObject.description);
        Console.WriteLine(x.TheObject.name);
    }
    public class XmlDynamicModel
    {
        public XmlDynamicModel(string xmlfile)
        {
          this.TheObject = new ExpandoObject();
          var t = this.TheObject as IDictionary<String, object>;
          XDocument xmlDoc = XDocument.Load(xmlfile);
          //get all objects UNDER product
          foreach(var elem in xmlDoc.Descendants().Descendants())
          {
            t[elem.Name.ToString()] = elem.Value.ToString();
          }
        }
        public dynamic TheObject {get;set;}
    }
    

    You could make it fancier by adding the object name (in this case product) as a property and looking out for different types and setting null values etc.

    Hope it helps