none
EntityObject serialization RRS feed

  • Question

  • Is it possible to add [XmlAttribute]  to all properties of all entityobjects at the time when

    model is created from database ?

    I am having problem binding serialized entityobjects through XmlDatasource  to grids (radGrid and GridView as well)

    because those expect column names/values to be in element attributes.

    Thursday, June 16, 2011 7:39 PM

Answers

  • Hi!

    An option is add a code generation item to your project and modify this.

    - Open your entity model
    - Right click it and select "Add Code Generation Item..."
    - Select the "ADO.NET EntityObject Generator" and give it a reasonable name (eg. the same name as your .edmx file)

    You will now have .tt (T4 template) file which you can modify so it generates your classes as you want.

    To give your primitive properties the [XmlAttribute] you can insert [XmlAttribute] at line 557, and all primitive properties in your entityobject will have this attribute. For complext type properties, insert [XmlAttribute] at line 636 (if you added the one on 557), and for  navigation properties around 354. Then you will have [XmlAttribute] on all properties.

    Every time you change your model, the .tt template will automatically generate and update your entity object.

    Hope this helps!

     


    --Rune
    • Marked as answer by vl2 Friday, June 17, 2011 3:50 PM
    Thursday, June 16, 2011 9:13 PM

All replies

  • On 6/16/2011 3:39 PM, vl2 wrote:
    > Is it possible to add [XmlAttribute] to all properties of all
    > entityobjects at the time when
    >
    > model is created from database ?
    >
    > I am having problem binding serialized entityobjects through
    > XmlDatasource to grids (radGrid and GridView as well)
     >
    > because those expect column names/values to be in element attributes.
    >
     
    You should not have to do that. The EF entities are bindable as is when
    results are returned in a collection.
     
    I don't know how you came to the conclusion you need XMLDatasource.
     
     
    Thursday, June 16, 2011 7:49 PM
  • I can not do that because the requirement is to decouple UI from all layers and use XML or JSON to do data exchange.

    That is not the best approach anyway but we already designed the system on top of EF to use it the way you are saying when

    suddenly there was a proposal to change that to support various clients not just .net UI and not only Windows clients. Just trying to figure

    out the fastest way to do that.


    Thursday, June 16, 2011 8:02 PM
  •  "to support various clients" maybe you can use WCF Data Service!

    Regards

    Thursday, June 16, 2011 8:13 PM
  • Yes you are correct  but my question was about binding resulting xml to gridview.

     

    Thursday, June 16, 2011 9:02 PM
  • Hi!

    An option is add a code generation item to your project and modify this.

    - Open your entity model
    - Right click it and select "Add Code Generation Item..."
    - Select the "ADO.NET EntityObject Generator" and give it a reasonable name (eg. the same name as your .edmx file)

    You will now have .tt (T4 template) file which you can modify so it generates your classes as you want.

    To give your primitive properties the [XmlAttribute] you can insert [XmlAttribute] at line 557, and all primitive properties in your entityobject will have this attribute. For complext type properties, insert [XmlAttribute] at line 636 (if you added the one on 557), and for  navigation properties around 354. Then you will have [XmlAttribute] on all properties.

    Every time you change your model, the .tt template will automatically generate and update your entity object.

    Hope this helps!

     


    --Rune
    • Marked as answer by vl2 Friday, June 17, 2011 3:50 PM
    Thursday, June 16, 2011 9:13 PM
  • On 6/16/2011 5:02 PM, vl2 wrote:
    > Yes you are correct but my question was about binding resulting xml to
    > gridview.
    >
     
    What XML? You are binding objects to the grid and not XML. If you want
    to bind XML, then you have to serialize the entities/objects to XML
    objects and bind the XML serialized objects. This makes no sense what
    you are doing, since the objects can be bound to the control without
    them being serialized XML objects.
     
    And if you wanted to bind XML to a control, then you would be using
    Linq-2-XML, which would encapsulate the XML as objects and bind the
    objects to the control.
     
    Thursday, June 16, 2011 9:14 PM
  • On 6/16/2011 5:02 PM, vl2 wrote:
    > Yes you are correct but my question was about binding resulting xml to
    > gridview.
    >
     
    What XML? You are binding objects to the grid and not XML. If you want
    to bind XML, then you have to serialize the entities/objects to XML
    objects and bind the XML serialized objects. This makes no sense what
    you are doing, since the objects can be bound to the control without
    them being serialized XML objects.
     
    And if you wanted to bind XML to a control, then you would be using
    Linq-2-XML, which would encapsulate the XML as objects and bind the
    objects to the control.
     

     I can not reference any EntityObjects from UI, I can not reference Datasets etc, because the requirement is to decouple UI from all layers. How would you bind

    EnityObjects in IPhone app or PHP client?

    Friday, June 17, 2011 2:44 AM

  • Hi!

    An option is add a code generation item to your project and modify this.

    - Open your entity model
    - Right click it and select "Add Code Generation Item..."
    - Select the "ADO.NET EntityObject Generator" and give it a reasonable name (eg. the same name as your .edmx file)

    You will now have .tt (T4 template) file which you can modify so it generates your classes as you want.

    To give your primitive properties the [XmlAttribute] you can insert [XmlAttribute] at line 557, and all primitive properties in your entityobject will have this attribute. For complext type properties, insert [XmlAttribute] at line 636 (if you added the one on 557), and for  navigation properties around 354. Then you will have [XmlAttribute] on all properties.

    Every time you change your model, the .tt template will automatically generate and update your entity object.

    Hope this helps!

     


    --Rune

    that lead me into another problem XmlAttributes and nullable types, Getting an error: Cannot serialize member 'JobPositionsCount' of type System.Nullable`1[System.Int64]. XmlAttribute/XmlText cannot be used to encode complex types.  I removed XmlAttribute from 636  and from 354


    but the error does not go away because  JobPositionsCount is nullable I guess.

    Friday, June 17, 2011 2:33 PM
  • Hi,

    I have never worked with these attributes, but after a quick search, I found this answer on stackoverflow http://stackoverflow.com/questions/2074240/serializing-a-nullabledatetime-in-to-xml,

    So.. in some way, you'll need to check if the type is a nullable, and then add xmlelement to those properties instead.


    --Rune
    Friday, June 17, 2011 3:00 PM
  • XmlElement can not be bound to GridView you need to have it as attribute. That was the original problem.  I guess will just have to write some reformating routine

    to convert XmlElements to attributes.

    Friday, June 17, 2011 3:49 PM