gpt4 book ai didi

c# - 解析具有相同名称元素的复杂 XML

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

在使用 LINQ 解析 XML 文件的过程中,我遇到了下一个问题。

总的来说,我目前的问题是我无法将具有相同名称的元素连接到索引元素。当我尝试将它插入数据库时​​出现问题,我得到的行比它应该的多。

应该如何:

uid |          lastName         |                         addressList                                |
-----------------------------------------------------------------------------------------------------|
36 | AEROCARIBBEAN AIRLINES | Havana Cuba |
306 | ANGLO-CARIBBEAN CO., LTD. | Zurich Switzerland; Madrid Spain; Tokyo Japan; Panama City Panama; |

我得到了什么:

uid |          lastName         |                         addressList                                |
-----------------------------------------------------------------------------------------------------|
36 | AEROCARIBBEAN AIRLINES | Havana Cuba |
306 | ANGLO-CARIBBEAN CO., LTD. | Zurich Switzerland; |
| | Madrid Spain; |
| | Tokyo Japan; |
| | Panama City Panama; |

XML:

<root>
<Entry>
<uid>36</uid>
<lastName>AEROCARIBBEAN AIRLINES</lastName>
<sdnType>Entity</sdnType>
<programList>
<program>CUBA</program>
</programList>
<akaList>
<aka>
<uid>12</uid>
<type>a.k.a.</type>
<category>strong</category>
<lastName>AERO-CARIBBEAN</lastName>
</aka>
</akaList>
<addressList>
<address>
<uid>25</uid>
<city>Havana</city>
<country>Cuba</country>
</address>
</addressList>
</Entry>
<Entry>
<uid>306</uid>
<lastName>BANCO NACIONAL DE CUBA</lastName>
<sdnType>Entity</sdnType>
<programList>
<program>CUBA</program>
</programList>
<akaList>
<aka>
<uid>219</uid>
<type>a.k.a.</type>
<category>weak</category>
<lastName>BNC</lastName>
</aka>
<aka>
<uid>220</uid>
<type>a.k.a.</type>
<category>strong</category>
<lastName>NATIONAL BANK OF CUBA</lastName>
</aka>
</akaList>
<addressList>
<address>
<uid>199</uid>
<address1>Zweierstrasse 35</address1>
<city>Zurich</city>
<postalCode>CH-8022</postalCode>
<country>Switzerland</country>
</address>
<address>
<uid>200</uid>
<address1>Avenida de Concha Espina 8</address1>
<city>Madrid</city>
<postalCode>E-28036</postalCode>
<country>Spain</country>
</address>
<address>
<uid>201</uid>
<address1>Dai-Ichi Bldg. 6th Floor, 10-2 Nihombashi, 2-chome, Chuo-ku</address1>
<city>Tokyo</city>
<postalCode>103</postalCode>
<country>Japan</country>
</address>
<address>
<uid>202</uid>
<address1>Federico Boyd Avenue &amp; 51 Street</address1>
<city>Panama City</city>
<country>Panama</country>
</address>
</addressList>
</Entry>
<root>

我的代码:

XDocument Document = XDocument.Load(@".\FileName.xml");

var RootNode = from row in Document.Root.Elements("Entry") select row;
var AdressNode = from row in Document.Root.Elements("Entry").Descendants("addressList").Descendants("address") select row;

string[] UIDArray = RootNode.Select(o => (string)o.Element("uid")).ToArray();
string[] FullNameArray = RootNode.Select(o => (string)o.Element("firstName") == null ? (string)o.Element("lastName") : (string)o.Element("firstName") + " " + (string)o.Element("lastName")).ToArray();

// addressList ? concat values?
string[] Test = AdressNode.Select(o => (string)o.Element("country") + " " + (string)o.Element("city") + " " + (string)o.Element("address1")).ToArray();

最佳答案

这是因为 AdressNode 已经填充了 5 个元素,所以您到达串联的团队为时已晚,因为所有后代都是从顶级“文档”中检索的。

相反,您可以从两个主要父元素中的每一个构建元素(即对于每个 Entry 元素)。

var RootNode = from row in Document.Root.Elements("Entry") select row;

var output = new List<string>();
foreach (var node in RootNode.ToList())
{
var AddressNode = from row in node.Descendants("addressList").Descendants("address") select row;
var element = AddressNode.Select(o => (string)o.Element("country") + " " + (string)o.Element("city") + " " + (string)o.Element("address1")).ToArray();
output.Add(string.Join(",", element.ToList()));
}

string[] Test = output.ToArray();

关于c# - 解析具有相同名称元素的复杂 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48724471/

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