C# 用xml访问列表

C# 用xml访问列表,c#,xml,C#,Xml,XML数据结构: <CPT xmlns="http://www.example.org/genericClientProfile" xmlns:ns2="http://www.bosch-si.com/finance/cts/cpt/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/genericClientProfile genericCl

XML数据结构:

<CPT xmlns="http://www.example.org/genericClientProfile" xmlns:ns2="http://www.bosch-si.com/finance/cts/cpt/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/genericClientProfile genericClientProfile.xsd">
<header>
    <serviceId>CPT-UK</serviceId>
    <versionId>1.0</versionId>
    <brandCode>RBS.ADA</brandCode>
    <creationTime>2013-09-24T16:56:52.985+02:00</creationTime>
</header>
<clientProfile bpKey="19933" id="1bb26568-1df3-4206-8cea-fb4614bf0a6a" createdBy="BARBOURT" lastModifiedBy="MANNC" documentStructureVersion="V3_2" lastModifiedAt="2013-09-23 15:40:49.873" createdAt="2013-09-23 10:07:33.608">
<section xmlns="" id="cd21fbb5-da1b-485d-8909-a8392fd5ad5c" name="globalClientFacts">
    <attribute name="familyAndDependentComment" type="string">Tomas is 18 in 2013</attribute>
<list name="familyAndDependents">
    <entry entryId="1">
        <attribute name="famDependAge" type="decimal">0</attribute>
        <attribute name="famDependBirthdate" type="date" />
        <attribute name="famDependFinDep" type="boolean">false</attribute>
        <attribute name="famDependFinDepUntil" type="string" />
        <attribute name="famDependName" type="string">Tomas</attribute>
        <attribute name="famDependNat" type="xmlCountry">GB</attribute>
        <attribute name="famDependRel" type="enumeration">SON</attribute>
        <attribute name="famDependRelTo" type="string" />
    </entry>
<entry entryId="2"></list>
<attribute name="subCommentWills" type="string" />
</section>
我有下面的数据结构,我想做的是从每个部分创建SQL insert语句。但是,如果一个节包含一个列表,我需要获取列表名,然后只获取其中一个条目的属性。目前,它似乎将属性加倍,因为有两个条目

C:


我不能100%确定XML文件的模式,但下面的方法行吗

将上一条foreach语句替换为以下语句:

foreach (var p in f.Elements("attribute")
                   .Union(f.Elements("list")
                           .SelectMany(l => l.Elements()
                                             .First()
                                             .Elements("attribute"))))
{
   //...
}
这将为您提供section元素的所有属性子元素,以及作为您的节的子元素的所有列表元素的第一个子元素的所有属性子元素


这有意义吗?

为什么有。选择e=>e?你能给出更多关于你给出的样本数据的实际结果的细节吗?不清楚。我已经编辑了你的标题。请看,如果一致意见是否定的,他们就不应该这样做。而且,您给出的代码甚至不会编译,这意味着显然不是代码将属性加倍。如果没有看到一些真正再现问题的代码,就很难帮助您。如果您可以生成一个简短但完整的程序来演示问题,那将是理想的。使用实际使用的方法进行更新。抱歉,刚刚意识到,我忘记了包含If语句,检查name属性。。如果我只想从第一个条目返回列表属性呢?f.Elementslist.SelectManyl=>l.Elements.first.Elementsattribute
foreach (var p in f.Elements("attribute")
                   .Union(f.Elements("list")
                           .SelectMany(l => l.Elements()
                                             .First()
                                             .Elements("attribute"))))
{
   //...
}