简体   繁体   中英

I am trying to return XML data from Entity Framework

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:

在此处输入图片说明

There is no sign of my XML?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM