Linq to XML基于子节点Attribute创建对象

时间:2017-04-13 11:34:06

标签: c# linq linq-to-xml

下面是我的XML

<Test>
<Collection>
<abc Name="EmployeeId>1</abc>
<abc Name="EmployeeName>Test</abc>
 </Collection>
    <Collection>
<abc Name="EmployeeId "/>
<abc Name="EmployeeName"/>
  </Collection>
    <Collection>
<abc Name="InstituteId" />
<abc Name="InstituteName"/>
<abc Name="InstituteLocation"/>
  </Collection>
       <Collection>
<abc Name="InstituteId">1</abc>
<abc Name="InstituteName">Test Institute </abc>
<abc Name="InstituteLocation">Test Location</abc>

  </Collection>
</Test>

以下是我的课程。

Public class Employee
{
      Public int Id { get; set; }
      Public string Name { get; set; }
}

Public class Institution
{
      Public int Id { get; set;}
      Public string Name {get; set; }
      Public string Location {get; set;}
}

如何按属性名创建类,如果属性名是instituteId而不是create Institute对象,或者创建Employee对象

1 个答案:

答案 0 :(得分:0)

试试xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication49
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<XElement> collections = doc.Descendants("Collection").ToList();

            foreach (XElement collection in collections)
            {
                List<XElement> abcs = collection.Elements("abc").ToList();

                if (((string)abcs[0].Attribute("Name")).StartsWith("Employee"))
                {
                    Employee newEmployee = new Employee();
                    Employee.employees.Add(newEmployee);
                    newEmployee.Id = abcs.Where(x => (string)x.Attribute("Name") == "EmployeeId").Select(x => (x.IsEmpty) ? null : (int?)x).FirstOrDefault();
                    newEmployee.Name  = (string)abcs.Where(x => (string)x.Attribute("Name") == "EmployeeName").FirstOrDefault();

                }
                if (((string)abcs[0].Attribute("Name")).StartsWith("Institute"))
                {
                    Institution newInstitution = new Institution();
                    Institution.institutions.Add(newInstitution);
                    newInstitution.Id = (int?)abcs.Where(x => (string)x.Attribute("Name") == "InstituteId").Select(x => (x.IsEmpty) ? null : (int?)x).FirstOrDefault();
                    newInstitution.Name = (string)abcs.Where(x => (string)x.Attribute("Name") == "InstituteName").FirstOrDefault();
                    newInstitution.Location = (string)abcs.Where(x => (string)x.Attribute("Name") == "InstituteLocation").FirstOrDefault();
                }
            }

        }

    }
    public class Employee
    {
        public static List<Employee> employees = new List<Employee>();
        public int? Id { get; set; }
        public string Name { get; set; }
    }

    public class Institution
    {
        public static List<Institution> institutions = new List<Institution>();
        public int? Id { get; set;}
        public string Name {get; set; }
        public string Location {get; set;}
    }
}