0

My apologies if this is a duplicate as there are many questions out there similar, but not quite what I'm looking for.

This is what I've written:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Xml.Serialization;
using DeptDataAccess;

namespace Basware_Web_Service.Controllers
{
    public class DepartmentDataController : ApiController
    {
        public IEnumerable<Department> Get()
        {
            using (DeptDBEntities entities = new DeptDBEntities())
            {
                return entities.Departments.ToList();
            }
        }

This is what is generated:

<ArrayOfDepartment xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DeptDataAccess">
<Department>
<Company>TEST_CO</Company>
<ID>1</ID>
<Text_1>0</Text_1>
<Text_2>Sample_1</Text_2>
</Department>
<Department>
<Company>TEST_CO</Company>
<ID>2</ID>
<Text_1>1</Text_1>
<Text_2>Sample_2</Text_2>
</Department>
<!--Additional Department elements omitted-->
</ArrayOfDepartment>

What I'm trying to do is replace the root element from ArrayOfDeparment to Document Element and the loop element from Department to Item.

I've tried adding XmlArrayItem, XmlRoot but keep getting errors and/or continuing to see ArrayOf*

dbc
  • 104,963
  • 20
  • 228
  • 340
Adam
  • 45
  • 4
  • To also note, DeptDataAccess is a data entity pulling from SQL Server. Don't really know if that matters, but if you've any questions, I'll be standing by to answer whatever I can. Thank you all in advance! – Adam Oct 01 '18 at 13:16
  • You'll probably need to write your own XmlSerializer and include that in Global.asax or WebApiConfig. [This](https://learn.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/json-and-xml-serialization#xml-media-type-formatter) might help – willwolfram18 Oct 01 '18 at 13:19
  • Take a look at [How to change XML root name with XML Serialization?](https://stackoverflow.com/a/2125258) (my preferred solution) and [Get rid of root element when serializing array](https://stackoverflow.com/q/5885145). In fact I think this is a duplicate. Agree? – dbc Oct 01 '18 at 14:11
  • Wait, no -- what serializer are you using? From the XML namespace `"http://schemas.datacontract.org/2004/07/DeptDataAccess"` it looks like you may be using `DataContractSerializer` not `XmlSerializer`. Can you confirm? – dbc Oct 01 '18 at 14:13

1 Answers1

0

Apologies for the delay in response... The data I was using was from a ADO.NET connection to my SQL Server DB. My attempts at trying to serialize an Array was never going to work so I ended up creating a "ParsedDeptData" Class to serialize and redefine the elements for delivery.

My parsed data class ended up looking like this:

using DeptDataAccess;
using System.Xml.Serialization;

namespace TempNamespace.DepartmentPClass
{
    [XmlType("DocumentElement")]

    public class Items : List<Item>
    {
        public Items()
        {

            using (DeptDBEntities entities = new DeptDBEntities())
            {
                foreach (Department entity in entities.Departments)
                {
                    Item item = new Item(entity);
                    this.Add(item);
                }
            }
        }
    }

    public class Item
    {
        public int ID { get; set; }
        public string Text_1 { get; set; }
        public string Text_2 { get; set; }
        public string Company { get; set; }

        public Item()
        {
            this.ID = 0;
            this.Text_1 = string.Empty;
            this.Text_2 = string.Empty;
            this.Company = string.Empty;
        }

        public Item(int id, string text_1, string text_2, string company)
        {
            this.ID = id;
            this.Text_1 = text_1;
            this.Text_2 = text_2;
            this.Company = company;
        }

        public Item(Department entity)
        {
            this.ID = entity.ID;
            this.Text_1 = entity.Text_1;
            this.Text_2 = entity.Text_2;
            this.Company = entity.Company;
        }
    }
}

I then adjusted my controller to be this:

namespace Basware_Web_Service.Controllers
{
    public class DepartmentDataController : ApiController
    {
        public DepartmentPClass.Items Get()
        {

            DepartmentPClass.Items items = new DepartmentPClass.Items();

            return items;
        }
    }
}

Lesson learned was that, to my knowledge, you cannot serialize a method and need to make your adjustments further up stream.

Adam
  • 45
  • 4