I am trying to return XML data from Entity Framework

c# entity-framework-6 xml

Question

I have create a MVC5 web app using Web API. I also use Entity Framework 6.

I have a stored procedure:

SELECT * FROM [Group] WHERE CompanyRef = @CompanyRef OR CompanyRef = '00000000-0000-0000-0000-000000000000' AND Active = 1 
FOR XML PATH('Group'), ROOT ('Groups')

It returns this in the query analyzer:

<Groups>
  <Group>
    <GroupId>1</GroupId>
    <GroupRef>00000000-0000-0000-0000-000000000000</GroupRef>
    <Name>Todays Work</Name>
    <Description>System</Description>
    <CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef>    
    <Active>1</Active>
  </Group>
  <Group>
    <GroupId>2</GroupId>
    <GroupRef>00000000-0000-0000-0000-000000000000</GroupRef> 
    <Name>All</Name>
    <Description>System</Description>
    <CompanyRef>00000000-0000-0000-0000-000000000000</CompanyRef> 
    <Active>1</Active>
  </Group>
</Groups>

The return type declared in Function Imports (EF) is a string.

In my services class I 'connect' to the Entity Model by calling this:

public ObjectResult<string> Get(Guid? companyRef)
{
    return o.Group_Get(companyRef);
}

In my API Controller I call my Services Function like this:

Compression compression = new Compression();
var test = groupRepository.Get(companyRef);

Inspecting the value of 'test' I get this:

enter image description here

There is no sign of my XML?

1
1
11/8/2015 12:39:41 PM

Popular Answer

You can use that:

 using (SqlConnection con = new SqlConnection(_dbContext.Database.Connection.ConnectionString))
        {
            #region Connection Open

            SqlCommand cmd = new SqlCommand("mbl_GetCourseMat", con);
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Par1", "par1");
            cmd.Parameters.AddWithValue("@Par2", 2);

            con.Open();

            #endregion

            #region Reader XML

            using (XmlReader reader = cmd.ExecuteXmlReader())
            {
                while (reader.Read())
                {
                    string result = reader.ReadOuterXml();
                    if (!String.IsNullOrEmpty(result))
                    {
                        XmlSerializer xs = new XmlSerializer(typeof(MobilCourseMatModel));
                        using (MemoryStream ms = new MemoryStream())
                        {
                            byte[] buffer = Encoding.UTF8.GetBytes(result);
                            ms.Write(buffer, 0, buffer.Length);
                            ms.Position = 0;
                            using (XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8))
                            {
                                model = (MobilCourseMatModel)xs.Deserialize(ms);
                            }
                        }
                    }
                }
            }

            #endregion

            con.Close();
        }
0
3/23/2016 1:23:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow