gpt4 book ai didi

使用具有属性的重复元素名称的 C# LINQ to XML 查询

转载 作者:太空宇宙 更新时间:2023-11-03 14:31:57 24 4
gpt4 key购买 nike

        <Party id="Party_1">
<PartyTypeCode tc="1">Person</PartyTypeCode>
<FullName>John Doe</FullName>
<GovtID>123456789</GovtID>
<GovtIDTC tc="1">Social Security Number US</GovtIDTC>
<ResidenceState tc="35">New Jersey</ResidenceState>
<Person>
<FirstName>Frank</FirstName>
<MiddleName>Roberts</MiddleName>
<LastName>Madison</LastName>
<Prefix>Dr.</Prefix>
<Suffix>III</Suffix>
<Gender tc="1">Male</Gender>
<BirthDate>1974-01-01</BirthDate>
<Age>35</Age>
<Citizenship tc="1">United States of America</Citizenship>
</Person>
<Address>
<AddressTypeCode tc="26">Bill Mailing</AddressTypeCode>
<Line1>2400 Meadow Lane</Line1>
<Line2></Line2>
<Line3></Line3>
<Line4></Line4>
<City>Somerset</City>
<AddressStateTC tc="35">New Jersey</AddressStateTC>
<Zip>07457</Zip>
<AddressCountryTC tc="1">United States of America</AddressCountryTC>
</Address>
</Party>
<!-- *********************** -->
<!-- Insured Information -->
<!-- *********************** -->
<Party id="Party_2">
<PartyTypeCode tc="1">Person</PartyTypeCode>
<FullName>Dollie Robert Madison</FullName>
<GovtID>123956239</GovtID>
<GovtIDTC tc="1">Social Security Number US</GovtIDTC>
<Person>
<FirstName>Dollie</FirstName>
<MiddleName>R</MiddleName>
<LastName>Madison</LastName>
<Suffix>III</Suffix>
<Gender tc="2">Female</Gender>
<BirthDate>1996-10-12</BirthDate>
<Citizenship tc="1">United States of America</Citizenship>
</Person>
<!-- Insured Address -->
<Address>
<AddressTypeCode tc="26">Bill Mailing</AddressTypeCode>
<Line1>2400 Meadow Lane</Line1>
<City>Somerset</City>
<AddressStateTC tc="35">New Jersey</AddressStateTC>
<Zip>07457</Zip>
<AddressCountryTC tc="1">United States of America</AddressCountryTC>
</Address>
<Risk>
<!-- Disability Begin Effective Date -->
<DisabilityEffectiveStartDate>2006-01-01</DisabilityEffectiveStartDate>
<!-- Disability End Effective Date -->
<DisabilityEffectiveStopDate>2008-01-01</DisabilityEffectiveStopDate>
</Risk>
</Party>
<!-- ******************************* -->
<!-- Company Information -->
<!-- ****************************** -->
<Party id="Party_3">
<PartyTypeCode tc="2">Organization</PartyTypeCode>
<Organization>
<DTCCMemberCode>1234</DTCCMemberCode>
</Organization>
<Carrier>
<CarrierCode>105</CarrierCode>
</Carrier>
</Party>

这是我的代码,它不起作用,因为第 3 方不包含全名,如果我只返回名称属性,我知道 partyelements 包含 3 个方。有没有办法单独遍历每个标签?

        var partyElements = from party in xmlDoc.Descendants("Party")
select new
{
Name = party.Attribute("id").Value,
PartyTypeCode = party.Element("PartyTypeCode").Value,
FullName = party.Element("FullName").Value,
GovtID = party.Element("GovtID").Value,
};

最佳答案

按照这些思路怎么样? (更多的是想法/建议而不是实现,因为我不知道你的对象模型)

var parties = xmlDoc.Descendants("Party");
foreach(var party in parties)
{
int partyTypeCode = party.Element("PartyTypeCode").Value;
if(partyTypeCode == 1)
{
var person = personFactory.Build(party);
// do something
}
else if(partyTypeCode == 2)
{
var organization = companyFactory.Build(party);
// do something
}
}

在一个完美的多态世界中,您只有一个 PartyFactory 而没有 ifswitch 语句,但是一个 person party 和一个公司聚会完全不同。

关于使用具有属性的重复元素名称的 C# LINQ to XML 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2406692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com