2014年4月9日 星期三

[linq] LINQ To XML Part2


  1. var bench = new XElement("bench",
  2.                 new XElement("toolbox",
  3.                     new XElement("handtool", "Hammer"),
  4.                     new XElement("handtool", "Rasp")
  5.                     ),
  6.                 new XElement("toolboox",
  7.                     new XElement("handtool", "Saw"),
  8.                     new XElement("powertool", "Nailgun")
  9.                     ),
  10.                 new XComment("Be carrful with the nail gun")
  11.                 );
  12.  
  13.             foreach (XNode Node in bench.Nodes())
  14.             {
  15.                 Console.WriteLine(Node.ToString(SaveOptions.DisableFormatting) + ".");
  16.             }
  17.  
  18.             //訪問返回型態為xElement子節點
  19.             foreach (XElement e in bench.Elements())
  20.             {
  21.                 Console.WriteLine(e.Name + "=" + e.Value);
  22.             }
  23.  
  24.             //LINQ 查詢Nailgun
  25.             IEnumerable<string> query =
  26.                 from toolbox in bench.Elements()
  27.                 where toolbox.Elements().Any(tool => tool.Value == "Nailgun")
  28.                 select toolbox.Value;
  29.  
  30.             foreach (string e in query)
  31.             {
  32.                 Console.WriteLine(e);
  33.             }
  34.  
  35.             //將bench節點值直接設定成test
  36.             bench.SetValue("test");
  37.             Console.WriteLine(bench.ToString());
  38.  
  39.             //將第一個Node取代為XComment
  40.             XElement items = XElement.Parse("<items><one/><two/><three/></items>");
  41.             items.FirstNode.ReplaceWith(new XComment("One was here"));
  42.             Console.WriteLine(items.ToString(SaveOptions.DisableFormatting));
  43.  
  44.             #endregion
  45.  
  46.             #region xml practice
  47.  
  48.             //CURD XML
  49.             XElement xmls = XElement.Load("../../XML.xml");
  50.             Console.WriteLine(xmls.ToString());
  51.  
  52.             if (xmls.Element("name") != null)
  53.             {
  54.                 xmls.Element("name").Value = "Andy";
  55.             }
  56.             Console.WriteLine(xmls.ToString());
  57.  
  58.             //移除Andy
  59.             xmls.Elements().Where(e => e.Value == "Andy")
  60.                             .Remove();
  61.             Console.WriteLine(xmls.ToString());
  62.  
  63.             //移除phone
  64.             xmls.Elements().Where(e => e.Name == "phone")
  65.                 .Remove();
  66.             Console.WriteLine(xmls.ToString());

2014年4月8日 星期二

[linq] linq to Xml part1

新手學習!!

  1.  static void Main(string[] args)
  2.         {
  3.             //linq to Xml
  4.             string xml = @"<customer id='123' status='archi'>
  5.                            <firstname enabled='true'>andy</firstname>
  6.                            <lastname>chen</lastname>
  7.                            </customer>";
  8.             //字串解析
  9.             XElement customer = XElement.Parse(xml);
  10.             //customer 的所有元素
  11.             foreach (XElement child in customer.Elements())
  12.             {
  13.                 Console.WriteLine(child.Name);
  14.             }
  15.             //讀取屬性
  16.             XElement firstname = customer.Element("firstname");
  17.             bool enabled = (bool) firstname.Attribute("enabled");
  18.             Console.WriteLine(enabled);
  19.             //更新屬性
  20.             firstname.Attribute("enabled").SetValue(!enabled);
  21.            
  22.             //讀取屬性
  23.             XElement lastname = customer.Element("lastname");
  24.             //讀整個xml片段
  25.             Console.WriteLine(lastname);
  26.             //讀context 用 Value
  27.             Console.WriteLine(lastname.Value);
  28.             //設定lastname
  29.             lastname.SetValue("Sho");
  30.             //增加新元素
  31.             customer.Add(new XElement("test", 123));
  32.             //看全部XML
  33.             Console.WriteLine(customer);
  34.              
  35.         }

2014年4月6日 星期日

[Linq] 基礎 part1

就...該還的基礎~
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             //測試資料
  6.             string[] names = {"andy""Peter""candy""tom"};
  7.  
  8.             //linq query expression syntax
  9.             IEnumerable<string> filternames = names.Where(=> n.Length >= 4);
  10.             foreach (string name in filternames)
  11.             {
  12.                 Console.WriteLine(name);
  13.             }
  14.  
  15.             //chaining Query Operators
  16.             //有左到右的操作順序,篩選、排序、轉換
  17.             IEnumerable<string> query = names
  18.                 .Where(=> n.Contains("a"))
  19.                 .OrderBy(=> n.Length)
  20.                 .Select(=> n.ToUpper());
  21.  
  22.             foreach (string name in query)
  23.             {
  24.                 Console.WriteLine(name);
  25.             }
  26.  
  27.             //Fluent syntax
  28.             IEnumerable<string> filternames2 = from n in names
  29.                                                 where n.Contains("a")
  30.                                                 select n;
  31.             foreach (string name in filternames2)
  32.             {
  33.                 Console.WriteLine(name);
  34.             }
  35.  
  36.             //nature ordering
  37.             int[] numbers = {109876};
  38.             IEnumerable<int> firstthree = numbers.Take(3);  // 10 9 8
  39.             IEnumerable<int> lasttwo = numbers.Skip(3);     // 7 6
  40.             IEnumerable<int> reversed = numbers.Reverse();  // 6 7 8 9 10
  41.  
  42.             //結合陣列
  43.             int[] seq1 = {123};
  44.             int[] seq2 = {345};
  45.             IEnumerable<int> concat = seq1.Concat(seq2);    // 1 2 3 3 4 5
  46.             IEnumerable<int> union = seq1.Union(seq2);      // 1 2 3 4 5
  47.             foreach (int i in concat)
  48.             {
  49.                 Console.Write(i.ToString() + "|");
  50.             }
  51.  
  52.         }
  53.     }