2012年5月16日 星期三

[C#] Use Linq to read XML

Use Linq to read XML

First we wrote a function named Create_XML_file() to create a XML file for the following example.

/// <summary>
    /// 建立XML資料
    /// </summary>
    static void Create_XML_file()
    {
      XDocument doc = new XDocument
        (
        new XElement("Books" ,
          new XElement("book" , new XAttribute("PubYear" , "2008") ,
            new XElement("id" , "001") ,
            new XElement("name", "Visual C# 2008", new XAttribute("language", "CH") )),

          new XElement("book", new XAttribute("PubYear", "2009"),
            new XElement("id", "002"),
            new XElement("name", "終極7000單字,這本最強", new XAttribute("language", "EN"))),

          new XElement("book", new XAttribute("PubYear", "2010"),
            new XElement("id", "003"),
            new XElement("name", "Android案例開發完全講義", new XAttribute("language", "CH"))),

          new XElement("book", new XAttribute("PubYear", "2011"),
            new XElement("id", "004"),
            new XElement("name", "ASP.NET 4.0專題實務(上)", new XAttribute("language", "CH"))),

          new XElement("book", new XAttribute("PubYear", "2011"),
            new XElement("id", "005"),
            new XElement("name", "ASP.NET 4.0專題實務(下)", new XAttribute("language", "CH"))),

          new XElement("book", new XAttribute("PubYear", "2012"),
            new XElement("id", "006"),
            new XElement("name", "Star Wars : The Return of Jedi", new XAttribute("language", "EN")))

      ));
      doc.Save("example.xml");
    }
The XML would be looked like this …



Then we created four functions that using Linq to read the XML.
The purposes of these functions were written in the comment.

/// <summary>
    /// Output all the books' information
    /// </summary>
    static void Print_Data_01()
    {
      XDocument _doc = XDocument.Load("example.xml");
     
      //Method 1
      IEnumerable<XElement> _elms =
        _doc.Elements("Books").Elements("book");

      /*Method 2
      var _elms = from x in _doc.Elements("Books").Elements()
                  select x;
      */

      foreach (XElement _elm in _elms)
      {
        Console.WriteLine(
          String.Format("ID:{0},TITLE:{1},PUBLISH YEAR:{2},LANGUAGE:{3} ",
          _elm.Element("id").Value,
          _elm.Element("name").Value,
          _elm.Attribute("PubYear").Value,
          _elm.Element("name").Attribute("language").Value ));
      }
    }
Result of Print_Data_01 :
編號:001,書名:Visual C# 2008,發行年份:2008,語言:CH
編號:002,書名:終極7000單字,這本最強,發行年份:2009,語言:EN
編號:003,書名:Android案例開發完全講義,發行年份:2010,語言:CH
編號:004,書名:ASP.NET 4.0專題實務(上),發行年份:2011,語言:CH
編號:005,書名:ASP.NET 4.0專題實務(下),發行年份:2011,語言:CH
編號:006,書名:Star Wars : The Return of Jedi,發行年份:2012,語言:EN

    /// <summary>
    /// Only output the required book of certain title
    /// </summary>
    static void Print_Data_02(String sBookName)
    {
      XDocument _doc = XDocument.Load("example.xml");

      var _elms = from x in _doc.Elements("Books").Elements("book")
                  where x.Elements("name").ElementAtOrDefault(0).Value == sBookName
                  select x;

      foreach (XElement _elm in _elms)
      {
        Console.WriteLine(
          String.Format("ID:{0},TITLE:{1},PUBLISH YEAR:{2},LANGUAGE:{3} ",
          _elm.Element("id").Value,
          _elm.Element("name").Value,
          _elm.Attribute("PubYear").Value,
          _elm.Element("name").Attribute("language").Value));
      }
    }
Result of Print_Data_02 :
編號:003,書名:Android案例開發完全講義,發行年份:2010,語言:CH

    /// <summary>
    /// Only output the books of certain language
    /// </summary>
    static void Print_Data_03(String sLanguage)
    {
      XDocument _doc = XDocument.Load("example.xml");

      var _elms = from x in _doc.Elements("Books").Elements("book")
            where x.Elements("name").Attributes("language").ElementAtOrDefault(0).Value == sLanguage
            select x;

      foreach (XElement _elm in _elms)
      {
        Console.WriteLine(
          String.Format("ID:{0},TITLE:{1},PUBLISH YEAR:{2},LANGUAGE:{3} ",
          _elm.Element("id").Value,
          _elm.Element("name").Value,
          _elm.Attribute("PubYear").Value,
          _elm.Element("name").Attribute("language").Value));
      }
    }
Result of Print_Data_03 :
編號:002,書名:終極7000單字,這本最強,發行年份:2009,語言:EN
編號:006,書名:Star Wars : The Return of Jedi,發行年份:2012,語言:EN

    /// <summary>
    /// Only output the books that were published after 2009 and were wrotten in Chinese
    /// </summary>
    static void Print_Data_04()
    {
      XDocument _doc = XDocument.Load("example.xml");

      var _elms = from x in _doc.Elements("Books").Elements("book")
            where
            int.Parse( x.Attributes("PubYear").ElementAtOrDefault(0).Value) >=2010
            &&
            x.Elements("name").Attributes("language").ElementAtOrDefault(0).Value == "CH"
            select x;

      foreach (XElement _elm in _elms)
      {
        Console.WriteLine(
          String.Format("ID:{0},TITLE:{1},PUBLISH YEAR:{2},LANGUAGE:{3} ",
          _elm.Element("id").Value,
          _elm.Element("name").Value,
          _elm.Attribute("PubYear").Value,
          _elm.Element("name").Attribute("language").Value));
      }
    }
Result of Print_Data_04 :
編號:003,書名:Android案例開發完全講義,發行年份:2010,語言:CH
編號:004,書名:ASP.NET 4.0專題實務(上),發行年份:2011,語言:CH
編號:005,書名:ASP.NET 4.0專題實務(下),發行年份:2011,語言:CH

沒有留言:

張貼留言