【问题标题】:XDocument reading child elementsXDocument 读取子元素
【发布时间】:2014-02-21 08:08:55
【问题描述】:

我刚刚开始在 C# 中使用 Linq to XML。我有一个包含书籍信息的 XML 文件。

XML 文件的结构如下:

<?xml version="1.0"?>
<catalog>
   <book id="bk112">
      <author>Galos, Mike</author>
      <title>Visual Studio 7: A Comprehensive Guide</title>
      <genre>Computer</genre>
      <price>49.95</price>
      <publish_date>2001-04-16</publish_date>
      <description>Microsoft Visual Studio 7 is explored in depth,
      looking at how Visual Basic, Visual C++, C#, and ASP+ are 
      integrated into a comprehensive development 
      environment.</description>
   </book>
</catalog>

我已经编写了代码,让我可以从 XML 文件中获取作者列表和书籍列表:

public List<string> GetBooks()
{
    XDocument document = XDocument.Load(XMLFileLocation);

    var query = from t in document.Descendants("title")
                select t.Value;

    return query.ToList<string>();
}

但是,我不知道如何继续制作一种可以让我获取有关特定书籍的信息的方法。例如:

GetBookAuthor("MyBook");

我该怎么办?

【问题讨论】:

    标签: c# xml linq linq-to-xml


    【解决方案1】:

    如果您想坚持使用 XDocument,这是一种通过书名获取作者的简单方法:

    public static string GetBookAuthor(XDocument xDoc, string title)
    {
        return xDoc.Root
            .Elements("book")
            .First(b => b.Element("title").Value == title)
            .Element("author")
            .Value;
    }
    

    不过我建议使用面向对象的方法:

    为什么不创建一个带有 Author 和 Title 属性的 Book 类,那么就不需要 GetBookAuthor 方法了?

    public class Book
    {
        public string Title { get; set; }
        public string Author { get; set; }
        // other Book properties ...
    }
    

    要获取 Book 对象的列表:

    public static List<Book> GetBooks()
    {
        XDocument document = XDocument.Load(xmlFile);
    
        var query = from t in document.Root.Elements("book")
                    select new Book()
                    {
                        Author = t.Element("author").Value,
                        Title = t.Element("title").Value
                    };
    
        return query.ToList();
    }
    

    然后您可以按名称返回 Book 对象:

    public static Book GetBook(List<Book> bookList, string title)
    {
        return bookList.First(b => b.Title == title);
    }
    

    并访问 Author 属性:

    var bookList = GetBooks()
    var author = GetBook(bookList, "MyBook").Author;
    

    现在,如果作者是更复杂的元素,您也可以创建一个 Author 类,等等。

    【讨论】:

      【解决方案2】:

      如果你想通过ID搜索:

      var author = document.Descendans("book")
         .Where(x => (string)x.Attribute("id") == id)
         .Select(x => (string)x.Element("author"))
         .FirstOrDefault();
      

      如果你想通过Title搜索:

      var author = document.Descendans("book")
         .Where(x => (string)x.Element("title") == title)
         .Select(x => (string)x.Element("author"))
         .FirstOrDefault();
      

      然后检查null并返回作者姓名:

      if(author != null)
         return author;
      

      【讨论】:

        猜你喜欢
        • 2016-06-24
        • 1970-01-01
        • 2012-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多