Using Linq to pull from IEnumerable and store in data table

前端 未结 3 1829
天命终不由人
天命终不由人 2021-01-16 05:23

I have a datatable that looks like the following

public static DataTable SetColumnHeaders(DataTable KeyDataTable)
{
    KeyDataTable.Columns.Add(\"First_Name         


        
3条回答
  •  [愿得一人]
    2021-01-16 06:12

    I prefer creating objects for each node level. It is easier to debug and test.

    Using this xml library.

    You'd create classes for each part, like:

    public class InsuredOrPrincipal 
    {
        XElement self;
        public InsuredOrPrincipal(XElement self) { this.self = self; }
    
        public GeneralPartyInfo GeneralPartyInfo { get { return _GeneralPartyInfo ?? (_GeneralPartyInfo = new GeneralPartyInfo(self.GetElement("GeneralPartyInfo"))); } }
        GeneralPartyInfo _GeneralPartyInfo;
    
        public InsuredOrPrincipalInfo InsuredOrPrincipalInfo 
        { get { return _InsuredOrPrincipalInfo ?? (_InsuredOrPrincipalInfo = new InsuredOrPrincipalInfo(self.GetElement("InsuredOrPrincipalInfo"))); } }
        InsuredOrPrincipalInfo _InsuredOrPrincipalInfo;
    }
    
    public class GeneralPartyInfo
    {
        XElement self;
        public GeneralPartyInfo(XElement self) { this.self = self; }
    
        public NameInfo NameInfo { get { return _NameInfo ?? (_NameInfo = new NameInfo(self.GetElement("NameInfo"))); } }
        NameInfo _NameInfo;
    
    }
    
    public class InsuredOrPrincipalInfo
    {
        XElement self;
        public InsuredOrPrincipalInfo(XElement self) { this.self = self; }
    
        public string InsuredOrPrincipalRoleCd
        {
            get { return self.Get("InsuredOrPrincipalRoleCd", string.Empty); }
        }
    }
    
    public class NameInfo
    {
        XElement self;
        public NameInfo(XElement self) { this.self = self; }
    
        public PersonName PersonName { get { return _PersonName ?? (_PersonName = new PersonName(self.GetElement("PersonName"))); } }
        PersonName _PersonName;
    }
    
    public class PersonName
    {
        XElement self;
        public PersonName(XElement self) { this.self = self; }
    
        public string Surname 
        { 
             get { return self.Get("Surname", string.Empty); }
             set { self.Set("Surname", value, false); }
        }
    }
    

    You would use it like this:

    foreach (FileInfo File in AutoDir.GetFiles())
    {
        DataRow fileRow = AutoDataTable.NewRow();
        XDocument xmlDoc = XDocument.Load(AutoDir + File.Name);
    
        InsuredOrPrincipal[] insured = xmlDoc.Root
            .Descendants("InsuredOrPrincipal")
            .Select(x => new InsuredOrPrincipal(x))
            .Where(ip => ip.InsuredOrPrincipalInfo.InsuredOrPrincipalRoleCd == "Insured")
            .ToArray();
    
        foreach(var person in insured)
        {
            string surname = person.GeneralPartyInfo.NameInfo.PersonName.Surname;
        }
    }
    

    Depending on your needs you can expand or shrink the number of classes and information per class as you need, but this is the way I'd go about it, as it makes more sense to me.

    Tested with this code:

    XElement test = new XElement("test");
    var ip = new InsuredOrPrincipal(test);
    ip.GeneralPartyInfo.NameInfo.PersonName.Surname = "Surname";
    test.Save(Path.Combine(Application.StartupPath, "insuredOrPrincipal.xml"));
    

    Which gave me the expected output:

    
    
      
        
          
            Surname
          
        
      
    
    

提交回复
热议问题