它看起来像这样:
[XmlRoot("Library")]
public class LibraryModel
{
[XmlElement("Book")]
public List<BookModel> Books { get; set; }
}
和
public class BookModel
{
[XmlElement(ElementName = "Title")]
public string Title { get; set; }
[XmlElement(ElementName = "Author")]
public string Author { get; set; }
[XmlElement(ElementName = "ISBN")]
public string ISBN { get; set; }
}
示例 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Library>
<Book>
<Title>The Name of the Wind</Title>
<Author>Patrick Rothfuss</Author>
<ISBN>9788580410631</ISBN>
</Book>
<Book>
<Title>The Wise Mans Fear</Title>
<Author>Patrick Rothfuss</Author>
<ISBN>9788834717790</ISBN>
</Book>
</Library>
我的输入是 BookModel 书。不知道最好有BookModel book之类的,还是字符串title、字符串作者、字符串ISBN:
BookModel book = new BookModel();
book.Title = "New title";
book.Author = "New author";
book.ISBN = "97839839033";
所以我的问题是,如何将新书(BookModel)放入 XML 文件,最后但在 XMLRoot 中?该课程将从 BookModel 更改为 Book 类型。
预期结果:
<?xml version="1.0" encoding="UTF-8"?>
<Library>
<Book>
<Title>The Name of the Wind</Title>
<Author>Patrick Rothfuss</Author>
<ISBN>9788580410631</ISBN>
</Book>
<Book>
<Title>The Wise Mans Fear</Title>
<Author>Patrick Rothfuss</Author>
<ISBN>9788834717790</ISBN>
</Book>
<Book>
<Title>New title</Title>
<Author>New author</Author>
<ISBN>97839839033</ISBN>
</Book>
</Library>