C# 将xml存储在对象中
比如,我有这样的xml文件:C# 将xml存储在对象中,c#,xml,xml-parsing,C#,Xml,Xml Parsing,比如,我有这样的xml文件: <?xml version="1.0"?> <catalog> <title>My book catalog</title> <link>http://example.com/catalog</link> <book id="bk101"> <author>Gambardella, Matthew</author> &
<?xml version="1.0"?>
<catalog>
<title>My book catalog</title>
<link>http://example.com/catalog</link>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
希望没有类似的问题,如果有,对不起,我的错,我没有找到。另外,如果有关于这个特殊案例的任何文档,请指向它,因为我就是找不到它。有大量关于xml解析的文档以各种方式提供,但没有关于这个案例的信息
提前感谢。以下是使用以下方法处理LinqToXml代码:
doc=XDocument.Load(“path/to/file.xml”);
列表项=doc.Root.Element(“目录”).Elements(“书籍”)。
选择(e=>新书(
(字符串)e.元素(“作者”)值,
(字符串)e.元素(“标题”).值,
(字符串)e.元素(“价格”).值,
(字符串)e.元素(“发布日期”).值,
(字符串)e.元素(“描述”).值,
(字符串)e.元素(“类型”).值
)).ToList();
你看到了吗?你说没有案件信息是什么意思????谷歌C#和Serialisationalready找到答案。将linq转换为xml与预定义的类一起使用,这些类包含适当的属性。如果需要,我可以发布源代码。
xml.title //must return "My book catalog"
xml.link //must return "http://example.com/catalog"
xml.book[0] //is an object with following properties:
//author, title, genre etc...
//i.e., xml.book[0].author must return Gambardella, Matthew
[DebuggerDisplay("{Title}")]
public class Catalog
{
XElement self;
public Catalog(XElement catalog) { self = catalog; }
public string Title { get { return self.Get("title", string.Empty); } }
public Uri Link { get { return self.Get<Uri>("link", null); } }
public Book[] Books
{
get { return _Books ?? (_Books = self.GetEnumerable("book", x => new Book(x)).ToArray()); }
}
Book[] _Books;
[DebuggerDisplay("{Title} by {Author}")]
public class Book
{
XElement self;
public Book(XElement book) { self = book; }
public string Id { get { return self.Get("id", string.Empty); } }
public string Author { get { return self.Get("author", string.Empty); } }
public string Title { get { return self.Get("title", string.Empty); } }
public string Genre { get { return self.Get("genre", string.Empty); } }
public decimal Price { get { return self.Get<decimal>("price", 0); } }
public DateTime PublishDate { get { return self.Get("publish_date", DateTime.MinValue); } }
public string Description { get { return self.Get("description", string.Empty); } }
}
}
Catalog catalog = new Catalog(XElement.Load(file)); // or .Parse(string)
doc = XDocument.Load("path/to/file.xml");
List<book> items = doc.Root.Element("catalog").Elements("book").
Select(e => new book(
(string)e.Element("author").Value,
(string)e.Element("title").Value,
(string)e.Element("price").Value,
(string)e.Element("publish_date").Value,
(string)e.Element("description").Value,
(string)e.Element("genre").Value
)).ToList();