2012-04-13 59 views
0

說,我有這樣的XML文件:存儲XML的對象

<?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.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 

希望沒有類似的問題和抱歉,如果有任何,我的壞,我沒有找到。另外,如果有任何有關這方面的文件,請特別指出,因爲我找不到它。有很多關於xml解析的文檔,但在這種情況下沒有任何信息。

在此先感謝。

+2

你見過[如何XML映射到C#對象(HTTP:/ /stackoverflow.com/questions/87621/how-do-i-map-xml-to-c-sharp-objects)? – miku 2012-04-13 21:12:54

+1

你是什麼意思沒有信息的情況下?谷歌C#和序列號 – 2012-04-13 21:12:55

+0

已經找出。將linq to xml與預定義的類一起使用,其中包含適當的屬性。如果需要,我可以發佈源代碼。 – cheshie 2012-04-13 23:55:10

回答

1

這裏使用these extension methods工作LinqToXml代碼:

[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) 
0
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();