gpt4 book ai didi

c# - XML 到字符串列表

转载 作者:数据小太阳 更新时间:2023-10-29 02:01:04 24 4
gpt4 key购买 nike

我有一些代码需要放入 C# 中的字符串列表中,我正在从 XML 文件中读取这段代码,它的布局如下所示...

<?xml version="1.0"?>
<accountlist>
<main>
<account id="1" special_id="4923959">
<username>Adam</username>
<motto>Hello Everyone>
<money>1004</money>
<friends>394</friends>
<rareid>9</rareid>
<mission>10</mission>
</account>
</main>
</accountlist>

如何将每个帐户标签放入一个字符串列表中?从第一个 标签?

请不要告诉我去下面的链接,因为它不起作用!! How to read a XML file and write into List<>?

到目前为止,我已经尝试了下面的代码,但字符串列表仍然是空的

XDocument doc = XDocument.Parse(this._accountsFile);

List<string> list = doc.Root.Elements("account")
.Select(element => element.Value)
.ToList();

this._accounts = list;

最佳答案

你必须使用 Descendants而不是 Elements :

List<string> list = doc.Root.Descendants("account").Descendants()
.Select(element => element.Value)
.ToList();

Elements只返回元素的子元素(如果是根元素,这意味着 <main>)。
Descendants返回元素内的整个树。

另外:您必须修复标签 <motto>Hello Everyone><motto>Hello Everyone</motto>

关于c# - XML 到字符串列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31532060/

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