【问题标题】:How to convert List<Model> to complex response type?如何将 List<Model> 转换为复杂响应类型?
【发布时间】:2017-02-13 15:44:43
【问题描述】:
    Public Class Employee
{
   Public String EmployeeId {get;set;}
   Public String EmployeeName {get;set;}
   Public String Department {get;set;}
}

Public Class Department
{
   Public String DepartmentId {get;set;}
   Public String DepartmentName {get;set;}
   Public String Address {get;set;}
}

Public Class Address
{
   Public String AddrOne {get;set;}
   Public String City {get;set;}
}

我有 3 个模型,List、List 和 List 程序执行后,上述 3 个模型应填充 List、List 和 List 我必须以以下格式返回数据...

获得以下格式响应的最佳方法是什么?

<Employees>
 <Employee>
   <EmployeeID>   </EmployeeID>
   <EmployeeName>   </EmployeeName>
   <Department>
     <DepartmentID>     </DepartmentID>
     <DepartmentName>     </DepartmentName>
    <Address>
      <Addr1>   </Addr1>
          <City>    </City>
    </Address>
   <Department>
 </Employee>
</Employees>

【问题讨论】:

  • @AfnanAhmad ,我正在循环所有 3 个集合(员工、部门和地址),基于它们的层次结构并创建 xml 结构。我相信有更好的方法来做到这一点......但没有任何线索 forrach(DataRow drEmp in ...) { xmlElement.Add(EmpID) xmlElement.Add(EmpNAme) forrach(DataRow drEmp in ...) { xmlElement.Add(DeptID) xmlElement.Add(DeptName) forrach(DataRow drEmp in ...) { xmlElement.Add(Addr1) } } }

标签: c# linq linq-to-xml


【解决方案1】:

您可以创建以下类:

public class Model
{
   public List<Employee> Employees { get; set; }
}

public class Employee
{
   public string EmployeeId { get; set; }
   public string EmployeeName { get; set; }
   public Department Department { get; set; }
}

public class Department
{
   public string DepartmentId { get; set; }
   public string DepartmentName {get; set; }
   public Address Address { get; set; }
}

public class Address
{
   public string AddrOne { get; set; }
   public string City { get; set; }
}

接下来,您可以创建模型实例并填充数据并将其序列化为 XML

【讨论】:

    【解决方案2】:

    XML 序列化:

    创建一个 CollectionClass 并添加方法来序列化它:

    public class MyCollection
    {
        public List<Employee> = new List<Employee>();
        public List<Department> = new List<Department>();
        public List<Address> = new List<Address>();
    
        public string ToXML()
        {
            var stringwriter = new System.IO.StringWriter();
            var serializer = new XmlSerializer(this.GetType());
            serializer.Serialize(stringwriter, this);
            return stringwriter.ToString();
        }
    
        // You have to use your Class-Type here 3 times
        public static MyCollection LoadFromXML(string filePath)
        {
            using (StreamReader streamReader = new System.IO.StreamReader(filePath))
            {
                var serializer = new XmlSerializer(typeof(MyCollection));
                return serializer.Deserialize(streamReader) as MyCollection;
            }
        }
    }
    

    现在您可以将类保存为 xml 文件:

            MyCollection myCollection = new MyCollection();
            //Now add your entries, myCollection.Add(new Department(....));  
    
            //Save your class as xml-File
            File.WriteAllText("C:\\MyClass.xml", myCollection.ToXML());
    

    然后你就可以加载它了:

            //Load your class
            MyCollection myCollection = MyCollection.LoadFromXML("C:\\MyClass.xml");
    

    编辑:将其更改为 CollectionClass-Sample,应该适合您的情况

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      相关资源
      最近更新 更多