- var bench = new XElement("bench",
- new XElement("toolbox",
- new XElement("handtool", "Hammer"),
- new XElement("handtool", "Rasp")
- ),
- new XElement("toolboox",
- new XElement("handtool", "Saw"),
- new XElement("powertool", "Nailgun")
- ),
- new XComment("Be carrful with the nail gun")
- );
- foreach (XNode Node in bench.Nodes())
- {
- Console.WriteLine(Node.ToString(SaveOptions.DisableFormatting) + ".");
- }
- //訪問返回型態為xElement子節點
- foreach (XElement e in bench.Elements())
- {
- Console.WriteLine(e.Name + "=" + e.Value);
- }
- //LINQ 查詢Nailgun
- IEnumerable<string> query =
- from toolbox in bench.Elements()
- where toolbox.Elements().Any(tool => tool.Value == "Nailgun")
- select toolbox.Value;
- foreach (string e in query)
- {
- Console.WriteLine(e);
- }
- //將bench節點值直接設定成test
- bench.SetValue("test");
- Console.WriteLine(bench.ToString());
- //將第一個Node取代為XComment
- XElement items = XElement.Parse("<items><one/><two/><three/></items>");
- items.FirstNode.ReplaceWith(new XComment("One was here"));
- Console.WriteLine(items.ToString(SaveOptions.DisableFormatting));
- #endregion
- #region xml practice
- //CURD XML
- XElement xmls = XElement.Load("../../XML.xml");
- Console.WriteLine(xmls.ToString());
- if (xmls.Element("name") != null)
- {
- xmls.Element("name").Value = "Andy";
- }
- Console.WriteLine(xmls.ToString());
- //移除Andy
- xmls.Elements().Where(e => e.Value == "Andy")
- .Remove();
- Console.WriteLine(xmls.ToString());
- //移除phone
- xmls.Elements().Where(e => e.Name == "phone")
- .Remove();
- Console.WriteLine(xmls.ToString());
2014年4月9日 星期三
[linq] LINQ To XML Part2
2014年4月8日 星期二
[linq] linq to Xml part1
新手學習!!
- static void Main(string[] args)
- {
- //linq to Xml
- string xml = @"<customer id='123' status='archi'>
- <firstname enabled='true'>andy</firstname>
- <lastname>chen</lastname>
- </customer>";
- //字串解析
- XElement customer = XElement.Parse(xml);
- //customer 的所有元素
- foreach (XElement child in customer.Elements())
- {
- Console.WriteLine(child.Name);
- }
- //讀取屬性
- XElement firstname = customer.Element("firstname");
- bool enabled = (bool) firstname.Attribute("enabled");
- Console.WriteLine(enabled);
- //更新屬性
- firstname.Attribute("enabled").SetValue(!enabled);
- //讀取屬性
- XElement lastname = customer.Element("lastname");
- //讀整個xml片段
- Console.WriteLine(lastname);
- //讀context 用 Value
- Console.WriteLine(lastname.Value);
- //設定lastname
- lastname.SetValue("Sho");
- //增加新元素
- customer.Add(new XElement("test", 123));
- //看全部XML
- Console.WriteLine(customer);
- }
2014年4月6日 星期日
[Linq] 基礎 part1
就...該還的基礎~
- class Program
- {
- static void Main(string[] args)
- {
- //測試資料
- string[] names = {"andy", "Peter", "candy", "tom"};
- //linq query expression syntax
- IEnumerable<string> filternames = names.Where(n => n.Length >= 4);
- foreach (string name in filternames)
- {
- Console.WriteLine(name);
- }
- //chaining Query Operators
- //有左到右的操作順序,篩選、排序、轉換
- IEnumerable<string> query = names
- .Where(n => n.Contains("a"))
- .OrderBy(n => n.Length)
- .Select(n => n.ToUpper());
- foreach (string name in query)
- {
- Console.WriteLine(name);
- }
- //Fluent syntax
- IEnumerable<string> filternames2 = from n in names
- where n.Contains("a")
- select n;
- foreach (string name in filternames2)
- {
- Console.WriteLine(name);
- }
- //nature ordering
- int[] numbers = {10, 9, 8, 7, 6};
- IEnumerable<int> firstthree = numbers.Take(3); // 10 9 8
- IEnumerable<int> lasttwo = numbers.Skip(3); // 7 6
- IEnumerable<int> reversed = numbers.Reverse(); // 6 7 8 9 10
- //結合陣列
- int[] seq1 = {1, 2, 3};
- int[] seq2 = {3, 4, 5};
- IEnumerable<int> concat = seq1.Concat(seq2); // 1 2 3 3 4 5
- IEnumerable<int> union = seq1.Union(seq2); // 1 2 3 4 5
- foreach (int i in concat)
- {
- Console.Write(i.ToString() + "|");
- }
- }
- }
訂閱:
文章 (Atom)