2017-04-13 108 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;} 
    } 
} 
相關問題