0

好的,所以我有一些课程如下...

/// <summary>
/// 
/// </summary>
public class InsuredOrPrincipal 
{
    XElement self;
    public InsuredOrPrincipal(XElement self) { this.self = self; }

    public GeneralPartyInfo GeneralPartyInfo { get { return _GeneralPartyInfo ?? (_GeneralPartyInfo = new GeneralPartyInfo(self.Element("GeneralPartyInfo"))); } }
    GeneralPartyInfo _GeneralPartyInfo;

    public InsuredOrPrincipalInfo InsuredOrPrincipalInfo 
    { get { return _InsuredOrPrincipalInfo ?? (_InsuredOrPrincipalInfo = new InsuredOrPrincipalInfo(self.Element("InsuredOrPrincipalInfo"))); } }
    InsuredOrPrincipalInfo _InsuredOrPrincipalInfo;
}
/// <summary>
/// 
/// </summary>
public class GeneralPartyInfo
{
    XElement self;
    public GeneralPartyInfo(XElement self) { this.self = self; }

    public NameInfo NameInfo { get { return _NameInfo ?? (_NameInfo = new NameInfo(self.Element("NameInfo"))); } }
    NameInfo _NameInfo;

    public Addr Addr { get { return _Addr ?? (_Addr = new Addr(self.Element("Addr"))); } }
    Addr _Addr;

    public string NameInfoStr
    {
        get { return (string)self.Element("NameInfo"); }
    }
}

我尝试测试是 nameInfoStr != null 并且只是使用以下代码块测试 NameInfo != null

InsuredOrPrincipal[] coInsured = xmlDoc.Root
           .Descendants("InsuredOrPrincipal")
           .Select(x => new InsuredOrPrincipal(x))
           .Where(ip => ip.InsuredOrPrincipalInfo.InsuredOrPrincipalRoleCd == "Coinsured")
           .Where( gp => gp.GeneralPartyInfo.NameInfo != null)
           .ToArray();

或 .Where(gp=> gp.GeneralPartyInfo.nameInfoStr != null

我使用 xmlDoc 作为 XDocument,我必须调用一个 InsuredOrPrincipal 数组,因为可能有很多。我需要测试名称节点是否存在于 xml 文档中。因为如果他们不这样做,那么我不想将该被保险人或原则添加到数组中。先感谢您

蒂姆

如果这有帮助,下面是额外的节点

public class NameInfo
{
    XElement self;
    public NameInfo(XElement self) { this.self = self; }

    public PersonName PersonName { get { return _PersonName ?? (_PersonName = new PersonName(self.Element("PersonName"))); } }
    PersonName _PersonName;
}

/// <summary>
/// 
/// </summary>
public class PersonName
{
    XElement self;
    public PersonName(XElement self) { this.self = self; }

    public string Surname
    {
        get { return (string)self.Element("Surname"); }
        set { self.Element("Surname").SetValue(value); }
    }
    public string GivenName
    {
        get { return (string)self.Element("GivenName"); }
        set { self.Element("GivenName").SetValue(value); }
    }
    public string OtherGivenName
    {
        get { return (string)self.Element("OtherGivenName"); }
        set { self.Element("OtherGivenName").SetValue(value); }
    }

    public string NickName
    {
        get { return (string)self.Element("NickName"); }
        set { self.Element("NickName").SetValue(value); }
    }

}

这是一个示例 XML

  InsuredOrPrincipal 
     GeneralPartyInfo 
                 NameInfo 
                 PersonName 
                 Surname NAME  Surname  
                 GivenName FIRST  GivenName  
                 OtherGivenName J  OtherGivenName  
              PersonName 
             TaxIdentity 
                 TaxIdTypeCd SSN  TaxIdTypeCd  
                 TaxId XXX-XX-XXXX  TaxId  
              TaxIdentity 
          NameInfo 
         Addr 
             AddrTypeCd MailingAddress  AddrTypeCd  
             Addr1 1315 STREET DR  Addr1  
             City Picker  City  
             StateProvCd OH  StateProvCd  
             PostalCode 99999  PostalCode  
             CountryCd Fairfield  CountryCd  
          Addr 
      GeneralPartyInfo 
     InsuredOrPrincipalInfo 
         InsuredOrPrincipalRoleCd Insured  InsuredOrPrincipalRoleCd  

      InsuredOrPrincipalInfo 
InsuredOrPrincipal 



  InsuredOrPrincipal
 GeneralPartyInfo 
     NameInfo 
         TaxIdentity 
             TaxIdTypeCd SSN  TaxIdTypeCd  
             TaxId 999-88-5684  TaxId  
          TaxIdentity 
      NameInfo 
  GeneralPartyInfo 
 InsuredOrPrincipalInfo 
     InsuredOrPrincipalRoleCd Coinsured  InsuredOrPrincipalRoleCd  
  InsuredOrPrincipalInfo 
InsuredOrPrincipal 

这是使用它的功能

private void CreateDataRows(XDocument xmlDoc, String Name)
    {
        DataRow fileRow = _ComboDataTable.NewRow();

        InsuredOrPrincipal[] insured = xmlDoc.Root
              .Descendants("InsuredOrPrincipal")
              .Select(x => new InsuredOrPrincipal(x))
              .Where(ip => ip.InsuredOrPrincipalInfo.InsuredOrPrincipalRoleCd == "Insured")
              .ToArray();

        InsuredOrPrincipal[] coInsured = xmlDoc.Root
           .Descendants("InsuredOrPrincipal")
           .Select(x => new InsuredOrPrincipal(x))
           .Where(ip => ip.InsuredOrPrincipalInfo.InsuredOrPrincipalRoleCd == "Coinsured")
           .ToArray();


        _insuredCount = insured.Count() + coInsured.Count(); //count number of elements 

        foreach (var person in insured)
        {
            try
            {
                fileRow["First_Name"] = person.GeneralPartyInfo.NameInfo.PersonName.GivenName;
            }
            catch
            {
                fileRow["First_Name"] = " ";
            }
            try
            {
                fileRow["Last_name"] = person.GeneralPartyInfo.NameInfo.PersonName.Surname;
            }
            catch
            {
                fileRow["Last_name"] = " ";
            }
            fileRow["Address1"] = person.GeneralPartyInfo.Addr.Address1;
            fileRow["City"] = person.GeneralPartyInfo.Addr.City;
            fileRow["State"] = person.GeneralPartyInfo.Addr.State;


            if (person.GeneralPartyInfo.Addr.Address2 != null)
                fileRow["Address2"] = person.GeneralPartyInfo.Addr.Address2;
            else
                fileRow["Address2"] = ' ';

            switch (person.GeneralPartyInfo.Addr.Zip.Length)
            {
                case 5:
                case 7: fileRow["Zip"] = person.GeneralPartyInfo.Addr.Zip.Substring(0, 5);
                    fileRow["Zip4"] = ' ';
                    break;
                case 6: fileRow["Zip"] = '0' + person.GeneralPartyInfo.Addr.Zip.Substring(0, 4);
                    fileRow["Zip4"] = ' ';
                    break;
                case 9: fileRow["Zip"] = person.GeneralPartyInfo.Addr.Zip.Substring(0, 5);
                    fileRow["Zip4"] = person.GeneralPartyInfo.Addr.Zip.Substring(5, 4); break;
                case 10: fileRow["Zip"] = person.GeneralPartyInfo.Addr.Zip.Substring(0, 5);
                    fileRow["Zip4"] = person.GeneralPartyInfo.Addr.Zip.Substring(6, 4); break;
                default: fileRow["Zip"] = "00000"; fileRow["Zip4"] = "0000"; break;
            }
            fileRow["Match_File"] = Name.ToString();
            fileRow["Num_Insured"] = _insuredCount.ToString();
            _ComboDataTable.Rows.Add(fileRow);
        }

        DataRow fileRow2 = _ComboDataTable.NewRow();

        foreach (var person2 in coInsured)
        {
            try
            {
                fileRow2["First_Name"] = person2.GeneralPartyInfo.NameInfo.PersonName.GivenName;
            }
            catch
            {
                fileRow2["First_Name"] = " ";
            }
            try
            {
                fileRow2["Last_name"] = person2.GeneralPartyInfo.NameInfo.PersonName.Surname;
            }
            catch
            {
                fileRow2["Last_name"] = " ";
            }
            try
            {
                fileRow2["Address1"] = person2.GeneralPartyInfo.Addr.Address1;
            }
            catch
            {
                fileRow2["Address1"] = fileRow["Address1"];
            }
            try
            {
                fileRow2["City"] = person2.GeneralPartyInfo.Addr.City;
            }
            catch
            {
                fileRow2["City"] = fileRow["City"];
            }

            try
            {
                fileRow2["State"] = person2.GeneralPartyInfo.Addr.State;
            }
            catch
            {
                fileRow2["State"] = fileRow["State"];
            }

            try
            {
                fileRow2["Address2"] = person2.GeneralPartyInfo.Addr.Address2;
            }
            catch
            {
                fileRow2["Address2"] = fileRow["Address2"];
            }

            try
            {
                switch (person2.GeneralPartyInfo.Addr.Zip.Length)
                {
                    case 5:
                    case 7: fileRow2["Zip"] = person2.GeneralPartyInfo.Addr.Zip.Substring(0, 5);
                        fileRow2["Zip4"] = ' ';
                        break;
                    case 6: fileRow2["Zip"] = '0' + person2.GeneralPartyInfo.Addr.Zip.Substring(0, 4);
                        fileRow2["Zip4"] = ' ';
                        break;
                    case 9: fileRow2["Zip"] = person2.GeneralPartyInfo.Addr.Zip.Substring(0, 5);
                        fileRow2["Zip4"] = person2.GeneralPartyInfo.Addr.Zip.Substring(5, 4); break;
                    case 10: fileRow2["Zip"] = person2.GeneralPartyInfo.Addr.Zip.Substring(0, 5);
                        fileRow2["Zip4"] = person2.GeneralPartyInfo.Addr.Zip.Substring(6, 4); break;
                    default: fileRow2["Zip"] = "00000"; fileRow2["Zip4"] = "0000"; break;
                }
            }
            catch
            {
                fileRow2["Zip"] = fileRow["Zip"];
                fileRow2["Zip4"] = fileRow["Zip4"];
            }

            fileRow2["Match_File"] = Name.ToString();
            fileRow2["Num_Insured"] = _insuredCount.ToString();
            _ComboDataTable.Rows.Add(fileRow2);
        }
    }

希望你能帮忙

4

0 回答 0