2017-04-13 133 views
0

下面是我的XML的LINQ to 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不是创造研究所的对象,否则创建Employee对象

+0

根据孩子的属性创建对象在C#中应该是一个简单的'if else'块。尝试实施,如果你还没有,显示你的尝试代码,并指出哪部分不按预期工作,或者你有麻烦实施正确... – har07

回答

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