如何使用 linq to xml 将列表保存在对象内?
how to save the List inside the object using linq to xml?
我正在尝试在 XDocument 中保存带有嵌套列表的对象列表。那么如何实现呢?
我有一个class:
public class Book
{
public string Id { get; }
public string Title { get; set; }
public string Isbn { get; set; }
public List<string> Authors { get; set; }
public int Pages { get; set; }
...
}
还有一个 class 来存储所有内容:
public class FileBookStore : IEntryStore<Book>
{
private List<Book> loadedBooks;
private string filename;
...
private static async Task<IEnumerable<Book>> ReadDataAsync(string filename)
{
...
IEnumerable<Book> result = XDocument.Parse(text)
.Root
.Elements("book")
.Select(e =>
new Book
{
Title = e.Attribute("title").Value,
Isbn = e.Attribute("isbn").Value,
Authors = new List<string>() //and here
});
return result;
}
static async Task SaveDataAsync(string filename, IEnumerable<Book> books)
{
XDocument root = new XDocument(
new XElement("catalog",
books.Select(n =>
new XElement("book",
new XAttribute("title", n.Title ?? ""),
new XAttribute("isbn", n.Isbn ?? ""),
//stuck in a line below
new XElement("authors", books.Select(n => new XElement("author"), new XAttribute("name"))),
new XAttribute("pages", n.Pages),
new XAttribute("year", n.Year),
new XAttribute("publisher", n.Publisher ?? "")))));
using (StreamWriter writer = new StreamWriter(filename))
{
await writer.WriteAsync(root.ToString()).ConfigureAwait(false);
}
}
我完全被这部分挂断了。如何保存和加载对象 to/from 集合?
更改卡住的线路:
//stuck in a line below
new XElement("authors", n.Authors.Select(x => new XElement("author", new XAttribute("name",x)))),
我正在尝试在 XDocument 中保存带有嵌套列表的对象列表。那么如何实现呢?
我有一个class:
public class Book
{
public string Id { get; }
public string Title { get; set; }
public string Isbn { get; set; }
public List<string> Authors { get; set; }
public int Pages { get; set; }
...
}
还有一个 class 来存储所有内容:
public class FileBookStore : IEntryStore<Book>
{
private List<Book> loadedBooks;
private string filename;
...
private static async Task<IEnumerable<Book>> ReadDataAsync(string filename)
{
...
IEnumerable<Book> result = XDocument.Parse(text)
.Root
.Elements("book")
.Select(e =>
new Book
{
Title = e.Attribute("title").Value,
Isbn = e.Attribute("isbn").Value,
Authors = new List<string>() //and here
});
return result;
}
static async Task SaveDataAsync(string filename, IEnumerable<Book> books)
{
XDocument root = new XDocument(
new XElement("catalog",
books.Select(n =>
new XElement("book",
new XAttribute("title", n.Title ?? ""),
new XAttribute("isbn", n.Isbn ?? ""),
//stuck in a line below
new XElement("authors", books.Select(n => new XElement("author"), new XAttribute("name"))),
new XAttribute("pages", n.Pages),
new XAttribute("year", n.Year),
new XAttribute("publisher", n.Publisher ?? "")))));
using (StreamWriter writer = new StreamWriter(filename))
{
await writer.WriteAsync(root.ToString()).ConfigureAwait(false);
}
}
我完全被这部分挂断了。如何保存和加载对象 to/from 集合?
更改卡住的线路:
//stuck in a line below
new XElement("authors", n.Authors.Select(x => new XElement("author", new XAttribute("name",x)))),