XML 集合 泛型

//-------------
//| ---XML----|
//-------------
//XmlElement theBook = null, theElem = null, root = null;
//XmlDocument xmldoc = new XmlDocument();
//try
//{
  
//    xmldoc.Load("Books.xml");
//    root = xmldoc.DocumentElement;

//    //---  新建一本书开始 ----
//    theBook = xmldoc.CreateElement("book");
//    theElem = xmldoc.CreateElement("name");
//    theElem.InnerText = "新书";
//    theBook.AppendChild(theElem);

//    theElem = xmldoc.CreateElement("price");
//    theElem.InnerText = "20";
//    theBook.AppendChild(theElem);

//    theElem = xmldoc.CreateElement("memo");
//    theElem.InnerText = "新书更好看。";
//    theBook.AppendChild(theElem);
//    root.AppendChild(theBook);
//    Console.Out.WriteLine("---  新建一本书开始 ----");
//    Console.Out.WriteLine(root.OuterXml);
//    //---  新建一本书完成 ----

//    //---  下面对《哈里波特》做一些修改。 ----
//    //---  查询找《哈里波特》----
//    theBook = (XmlElement)root.SelectSingleNode("/books/book[name='哈里波特']");
//    Console.Out.WriteLine("---  查找《哈里波特》 ----");
//    Console.Out.WriteLine(theBook.OuterXml);
//    //---  此时修改这本书的价格 -----
//    theBook.GetElementsByTagName("price").Item(0).InnerText = "15";//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,GetElementsByTagName("price")相当于SelectNodes(".//price")。
//    Console.Out.WriteLine("---  此时修改这本书的价格 ----");
//    Console.Out.WriteLine(theBook.OuterXml);
//    //---  另外还想加一个属性id,值为B01 ----
//    theBook.SetAttribute("id", "B01");
//    Console.Out.WriteLine("---  另外还想加一个属性id,值为B01 ----");
//    Console.Out.WriteLine(theBook.OuterXml);
//    //---  对《哈里波特》修改完成。 ----

//    //---  再将所有价格低于10的书删除  ----
//    theBook = (XmlElement)root.SelectSingleNode("/books/book[@id='B02']");
//    Console.Out.WriteLine("---  要用id属性删除《三国演义》这本书 ----");
//    Console.Out.WriteLine(theBook.OuterXml);
//    theBook.ParentNode.RemoveChild(theBook);
//    Console.Out.WriteLine("---  删除后的XML ----");
//    Console.Out.WriteLine(xmldoc.OuterXml);

//    //---  再将所有价格低于10的书删除  ----
//    XmlNodeList someBooks = root.SelectNodes("/books/book[price<10]");
//    Console.Out.WriteLine("---  再将所有价格低于10的书删除  ---");
//    Console.Out.WriteLine("---  符合条件的书有 " + someBooks.Count + "本。  ---");

//    for (int i = 0; i < someBooks.Count; i++)
//    {
//        someBooks.Item(i).ParentNode.RemoveChild(someBooks.Item(i));
//    }
//    Console.Out.WriteLine("---  删除后的XML ----");
//    Console.Out.WriteLine(xmldoc.OuterXml);

//    xmldoc.Save("books.xml");//保存到books.xml

//    Console.In.Read();
//}
//catch (Exception e)
//{
//    Console.Out.WriteLine(e.Message);
//}
//}

 

 

 

 

 


//泛型最常见的用途是泛型集合,命名空间System.Collections.Generic 中包含了一些基于泛型的集合类,使用泛型集合类可以提供更高的类型安全性,还有更高的性能,避免了非泛型集合的重复的装箱和拆箱。
//很多非泛型集合类都有对应的泛型集合类,下面是常用的非泛型集合类以及对应的泛型集合类:
//非泛型集合类 泛型集合类
//ArrayList List<T>
//HashTable DIctionary<T>
//Queue Queue<T>
//Stack Stack<T>
//SortedList SortedList<T>

//Dictionary
//Dictionary<string, string> myDic = new Dictionary<string, string>();
//myDic.Add("aaa", "111");
//myDic.Add("bbb", "222");
//myDic.Add("ccc", "333");
//myDic.Add("ddd", "444");
////如果添加已经存在的键,add方法会抛出异常
//try
//{
//    myDic.Add("ddd", "ddd");
//}
//catch (ArgumentException ex)
//{
//    Console.WriteLine("此键已经存在:" + ex.Message);
//}
////解决add()异常的方法是用ContainsKey()方法来判断键是否存在
//if (!myDic.ContainsKey("ddd"))
//{
//    myDic.Add("ddd", "ddd");
//}
//else
//{
//    Console.WriteLine("此键已经存在:");

//}

////而使用索引器来负值时,如果建已经存在,就会修改已有的键的键值,而不会抛出异常
//myDic["ddd"] = "ddd";
//myDic["eee"] = "555";

////使用索引器来取值时,如果键不存在就会引发异常
//try
//{
//    Console.WriteLine("不存在的键\"fff\"的键值为:" + myDic["fff"]);
//}
//catch (KeyNotFoundException ex)
//{
//    Console.WriteLine("没有找到键引发异常:" + ex.Message);
//}
////解决上面的异常的方法是使用ContarnsKey() 来判断时候存在键,如果经常要取健值得化最好用 TryGetValue方法来获取集合中的对应键值
//string value = null;
//if (myDic.TryGetValue("fff", out value))
//{
//    Console.WriteLine("不存在的键\"fff\"的键值为:" + value);
//}
//else
//{
//    Console.WriteLine("没有找到对应键的键值");
//}

////下面用foreach 来遍历键值对
////泛型结构体 用来存储健值对
//foreach (KeyValuePair<string, string> kvp in myDic)
//{
//    Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value);
//}
////获取值得集合
//foreach (string s in myDic.Values)
//{
//    Console.WriteLine("value={0}", s);
//}
////获取值得另一种方式
//Dictionary<string, string>.ValueCollection values = myDic.Values;
//foreach (string s in values)
//{
//    Console.WriteLine("value={0}", s);
//}

 

//SortedList类:表示键/值对的集合,与哈希表类似,区别在于SortedList中的Key数组排好序的
//SortedList sl = new SortedList();
//sl["c"] = 41;
//sl["a"] = 42;
//sl["d"] = 11;
//sl["b"] = 13;

//foreach (DictionaryEntry element in sl)
//{
//   string s = (string)element.Key;
//   int i = (int)element.Value;
//   Console.WriteLine("{0},{1}", s, i);
//}

 

//哈希表
//Hashtable ht = new Hashtable(); //创建一个Hashtable实例
//ht.Add("E", "e");//添加key/value键值对
//ht.Add("A", "a");
//ht.Add("C", "c");
//ht.Add("B", "b");

////遍历哈希表
//foreach (DictionaryEntry de in ht) //ht为一个Hashtable实例
//{
//    Console.WriteLine(de.Key);//de.Key对应于key/value键值对key
//    Console.WriteLine(de.Value);//de.Key对应于key/value键值对value
//}

////对哈希表进行排序在这里的定义是对key/value键值对中的key按一定规则重新排列,但是实际上这个定义是不能实现的,因为我们无法直接在Hashtable进行对key进行重新排列,如果需要Hashtable提供某种规则的输出,可以采用一种变通的做法:
//ArrayList akeys = new ArrayList(ht.Keys); //别忘了导入System.Collections
//akeys.Sort(); //按字母顺序进行排序
//foreach (string skey in akeys)
//{
//    Console.Write(skey + ":");
//    Console.WriteLine(ht[skey]);//排序后输出
//}

////ht.Add("A", "c");

//string s = (string)ht["A"];
//if (ht.Contains("E")) //判断哈希表是否包含特定键,其返回值为true或false
//    Console.WriteLine("the E key:exist");
//ht.Remove("C");//移除一个key/value键值对
//Console.WriteLine(ht["A"]);//此处输出a
//ht.Clear();//移除所有元素
//Console.WriteLine(ht["A"]); //此处将不会有任何输出

 

//Stack:栈,表示对象的简单的后进先出非泛型集合。Push方法入栈,Pop方法出栈。
//Stack sk = new Stack();
//Stack sk2 = new Stack();
//foreach (int i in new int[4] { 1, 2, 3, 4 })
//{
//    sk.Push(i);//入栈
//    sk2.Push(i);
//}

//foreach (int i in sk)
//{
//    Console.WriteLine(i);//遍历
//}

//sk.Pop();//出栈
//Console.WriteLine("Pop");
//foreach (int i in sk)
//{
//    Console.WriteLine(i);
//}

//sk2.Peek();//弹出最后一项不删除 
//Console.WriteLine("Peek");
//foreach (int i in sk2)
//{
//    Console.WriteLine(i);
//}

 


// Queue:队列,表示对象的先进先出集合。Enqueue方法入队列,Dequeue方法出队列。

//Queue qu = new Queue();
//Queue qu2 = new Queue();
//foreach (int i in new int[4] { 1, 2, 3, 4 })
//{
//qu.Enqueue(i);//入队
//qu2.Enqueue(i);
//}

//foreach (int i in qu)
//{
//Console.WriteLine(i);//遍历
//}

//qu.Dequeue();//出队
//Console.WriteLine("Dequeue");
//foreach (int i in qu)
//{
//Console.WriteLine(i);
//}

//qu2.Peek();//返回位于 Queue 开始处的对象但不将其移除。
//Console.WriteLine("Peek");
//foreach (int i in qu2)
//{
//Console.WriteLine(i);
//}


//ArrayList al = new ArrayList();
//al.Add(100);//单个添加
//foreach (int number in new int[6] { 9, 3, 7, 2, 4, 8 })
//{
//    al.Add(number);//集体添加方法一
//}
//int[] number2 = new int[2] { 11, 12 };
//al.AddRange(number2);//集体添加方法二
//al.Remove(3);//移除值为3的
//al.RemoveAt(3);//移除第3个
//ArrayList al2 = new ArrayList(al.GetRange(1, 3));//新ArrayList只取旧ArrayList一部份


//Console.WriteLine("遍历方法一:");
//foreach (int i in al)//不要强制转换
//{
//    Console.WriteLine(i);//遍历方法一
//}

//Console.WriteLine("遍历方法二:");
//for (int i = 0; i < al2.Count; i++)//数组是length
//{
//    int number = (int)al2[i];//一定要强制转换
//    Console.WriteLine(number);//遍历方法二

//}

 

//class Program
//{
//static void Main(string[] args)
//{
//    Country cy;

//    String assemblyName = @"MyTest";
//    string strongClassName = @"MyTest.China";
//    //必须强类名

//    cy = (Country)Assembly.Load(assemblyName).CreateInstance(strongClassName);

//    Console.WriteLine(cy.name);
//    Console.ReadKey();
//}
//}
//class Country
//{
//public string name;
//}
//class Chinese : Country
//{
//public Chinese()
//{
//    name = "你好";
//}
//}
//class America : Country
//{
//public America()
//{
//    name = "Hello";
//}
//}

posted @ 2009-07-06 00:55  Ry5  阅读(635)  评论(0编辑  收藏  举报