在Visual Studio C#中使用API​​控制器替换ArrayOf *根元素

时间:2018-10-01 13:14:37

标签: c# xml serialization xmlroot

如果这是重复的话,我道歉,因为那里有很多类似的问题,但不完全是我要寻找的问题。

这是我写的:

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

这是生成的:

<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>

我想做的是将ArrayOfDeparment的根元素替换为Document元素,将Department的loop元素替换为Item。

我尝试添加XmlArrayItem,XmlRoot,但一直出现错误和/或继续看到ArrayOf *

1 个答案:

答案 0 :(得分:0)

抱歉,响应延迟...我正在使用的数据是从ADO.NET连接到SQL Server DB的。我尝试序列化Array的尝试从未成功,所以我最终创建了一个“ ParsedDeptData”类来序列化和重新定义要交付的元素。

我解析的数据类最终看起来像这样:

library(tm)
text <- c("I am Mark I am Mark", "Neo4j is cool Neo4j is cool")
corpus <- VCorpus(VectorSource(text))

# wordLengths set to 3, basicly the default removes all words of length 1 and 2
tdm <- TermDocumentMatrix(corpus, control = list(wordLengths = c(3, Inf)))
as.matrix(tdm)

# only words cool and mark
# create a dictionary
my_dict <- c("cool", "mark")
tdm <- TermDocumentMatrix(corpus, control = list(dictionary = dict ))
as.matrix(tdm)
      Docs
Terms  1 2
  cool 0 2
  mark 2 0

然后我将控制器调整为:

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

经验教训是,据我所知,您无法序列化一种方法,需要进一步进行调整。