わかりましたので、次のようないくつかのクラスがあります...
/// <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"); }
}
}
テスト is 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
XDocument として xmlDoc を使用しています。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);
}
}
あなたが助けてくれることを願っています