Search code examples
c#sql-serverado.netpetapoco

How to retrieve xml column in PetaPoco


I was using nvarchar data type in sql (msql-server) to describe Description. But I would like to change the column to an xml data type instead. In my c# datalayer I'm using petapoco to get the data, which is using Ado.Net DataReader.

so

poco object:

[PetaPoco.TableName("sqlTableName")]
[PetaPoco.PrimaryKey("ID")]
public class PlainObj
{
    public int ID { get; set; } //(int, not null)
    public string Description { get; set; } //(string, null) want to change this to xml type
}

poco Get method

   public static List<PlainObj> Get(int InId)
    {
        var s = PetaPoco.Sql.Builder.Append(";EXEC @0", Common.StoreProcs.GetSP);
        s.Append("@@ID = @0", new SqlParameter() { SqlDbType = SqlDbType.Int, Value = InId });

        return PetaPocoContext.Fetch<PlainObj>(s); //Gets the object 
    }

My question is, how do I get XML instead of string for Description, and does PetaPoco supports it.


Solution

  • I don't think Petapoco supports XML data type out of the box, but if you change the data type to XML in SQL Server and keep the corresponding property on the poco as a string, you can use something like linq to xml or xDocument to handle the returned string as xml on selects. You could probably also add an XmlDocument property (Petapoco.Igrnored) to the poco (I would suggest in a partial class so it doesn't get over-written if you regen t4) and handle the string to xml & xml to string conversion in getters/setters so you have seamless access to the XML object. I haven't tested updates/inserts to an xml field yet though, but Petapoco's parameter handling usually takes anything I throw at it.