[英]Writing the contents of an ObservableCollection to a text file
The goal of this question is to find out a way to obtain specific contents of an ObservableCollection
( strings
to be specific), and write them to a text file. 这个问题的目标是找到一种方法来获取
ObservableCollection
特定内容(特定的strings
),并将它们写入文本文件。
This collection is structured by using nodes because it is used to implement a TreeView
. 此集合由使用节点构成,因为它用于实现
TreeView
。 With that being said I am currently trying to obtain the child values of a parent node like so: 话虽如此,我正在尝试获取父节点的子值,如下所示:
//I am currently getting an InvalidOperationException -- Sequence contains no matching element
var Node = Collection.GetAllChildren(x => x.Children).Distinct().ToList().First(x => x.DisplayName == Name);
In order to write to the file I am using this File.
为了写入我正在使用此
File.
method: File.WriteAllLines()
. 方法:
File.WriteAllLines()
。 It is my understanding that this method takes the FileName, and then an IEnumerable
(a List
, etc.). 我的理解是这个方法采用FileName,然后采用
IEnumerable
( List
等)。 Does ObservableCollection
contain a method that is IEnumberable
compatible or do I need to add the contents to a List
? ObservableCollection
是否包含与IEnumberable
兼容的方法,还是需要将内容添加到List
?
Right now I am trying this for the write to file line: 现在我正在尝试写入文件行:
File.WriteAllLines(FileName, new ObservableCollection<string> { ViewModel.Data.ToList() });
How can I correct these two detrimental parts of my code? 如何纠正我的代码中的这两个有害部分? What is the correct way to get the information I need, and then add it to the file?
获取所需信息,然后将其添加到文件的正确方法是什么?
Update: These are the current errors that I receive on the above^ line of code. 更新:这些是我在上面的代码行中收到的当前错误。 If I am implementing this correctly (which I am, based on the answers), why am I receiving these compile errors?
如果我正确地实现这个(我是基于答案),为什么我收到这些编译错误? (note:
type
has been switched to string
) (注意:
type
已切换为string
)
Update 2: I've updated the argument to be written to the file. 更新2:我已经更新了要写入文件的参数。
Data
is an ObservableCollection<ViewModel>
. Data
是ObservableCollection<ViewModel>
。
Yes, an overload of File.WriteAllLines
takes IEnumerable<string>
. 是的,
File.WriteAllLines
的重载需要IEnumerable<string>
。 ObservableCollection<string>
implements IEnumerable<string>
, so things should work. ObservableCollection<string>
实现IEnumerable<string>
,所以事情应该有效。
Just Type
should be string
: Just
Type
应该是string
:
File.WriteAllLines(FileName, new ObservableCollection<Type> ( NodeName.ToList() ));
Does ObservableCollection contain a method that is IEnumberable compatible
ObservableCollection是否包含与IEnumberable兼容的方法
No, it implements IEnumerable. 不,它实现了 IEnumerable。
PS As it is suggested in another answer, ObservableCollection
is redundant. PS正如在另一个答案中建议的那样,
ObservableCollection
是多余的。 You may pass anything which implements IEnumerable<string>
. 您可以传递任何实现
IEnumerable<string>
。
If you look at the signature of System.IO.File.WriteAllLines(), it accepts an array of strings or an IEnumerable of strings; 如果你看一下System.IO.File.WriteAllLines()的签名,它接受一个字符串数组或一个IEnumerable字符串;
Try this: 尝试这个:
File.WriteAllLines(FileName, NodeName.ToList());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.