我有一些代码需要放入 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>
如何将每个帐户标签放入字符串列表中?从第一个到标签?
请不要告诉我转到下面的链接,因为它不起作用!如何读取XML文件并写入List? https://stackoverflow.com/questions/9619324/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
返回元素内的整个树。
Also: 你必须修复标签<motto>Hello Everyone>
to <motto>Hello Everyone</motto>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)