【发布时间】:2019-07-04 11:48:17
【问题描述】:
将简单的 XML 文件加载到 LIST 中没有问题。但是,当我创建第二个元素时,它会加载,但会将所有内容加载到一行中。
我什至尝试使用 xmlDoc.Descendants("apple") 获得相同的结果。
有效。
<?xml version="1.0" encoding="utf-8"?>
<green_apple>
<Location>CA</Location>
<Price>.52</Price>
</green_apple>
XDocument xmlDoc = XDocument.Load("apple.xml");
List<string> list = xmlDoc.Root.Elements()
.Select(element => element.Value.Trim())
.ToList();
List Result:
list[0] = CA
List[1] = .52
不起作用。
<?xml version="1.0" encoding="utf-8"?>
<apple>
<green_apple>
<Location>CA</Location>
<Price>.52</Price>
</green_apple>
<red_apple>
<Location>FL</Location>
<Price>.71</Price>
</red_apple>
</apple>
XDocument xmlDoc = XDocument.Load("apple.xml");
List<string> list = xmlDoc.Root.Elements("green_apple") <<specify specify element.
.Select(element => element.Value.Trim())
.ToList();
List Result:
list[0] = CA.52 <<Here's the problem, they should be in their own list element.
【问题讨论】: