C Sharp và kiến trúc .NET. C Sharp cơ bản- P8 docx

5 226 0
C Sharp và kiến trúc .NET. C Sharp cơ bản- P8 docx

Đang tải... (xem toàn văn)

Thông tin tài liệu

XML in .NE XML in .NE T T - - Editor Editor : : Đo Đo à à n Quang n Quang Minh Minh 7 7 Đ Đ ọ ọ c c XML XML public class Sample public class Sample { { static void static void Main(string Main(string [] [] args args ) ) { { XmlTextReader XmlTextReader textReader textReader = new = new XmlTextReader("C: XmlTextReader("C: \ \ \ \ books.xml books.xml "); "); textReader.Read textReader.Read (); (); while ( while ( textReader.Read textReader.Read () ) // If the node has value () ) // If the node has value { { // Move to fist element // Move to fist element textReader.MoveToElement textReader.MoveToElement (); (); Console.WriteLine("XmlTextReader Console.WriteLine("XmlTextReader Properties Test"); Properties Test"); Console.WriteLine Console.WriteLine ("==================="); ("==================="); // Read this element's properties and display them on co // Read this element's properties and display them on co nsole nsole Console.WriteLine("Name Console.WriteLine("Name :" + :" + textReader.Name textReader.Name ); ); Console.WriteLine("Base Console.WriteLine("Base URI:" + URI:" + textReader.BaseURI textReader.BaseURI ); ); Console.WriteLine("Local Console.WriteLine("Local Name:" + Name:" + textReader.LocalName textReader.LocalName ); ); Console.WriteLine("Attribute Console.WriteLine("Attribute Count:" + Count:" + textReader.AttributeCount.ToString textReader.AttributeCount.ToString ()); ()); Console.WriteLine("Depth Console.WriteLine("Depth :" + :" + textReader.Depth.ToString textReader.Depth.ToString ()); ()); Console.WriteLine("Line Console.WriteLine("Line Number:" + Number:" + textReader.LineNumber.ToString textReader.LineNumber.ToString ()); ()); Console.WriteLine("Node Console.WriteLine("Node Type:" + Type:" + textReader.NodeType.ToString textReader.NodeType.ToString ()); ()); Console.WriteLine("Attribute Console.WriteLine("Attribute Count:" + Count:" + textReader.Value.ToString textReader.Value.ToString ()); ()); } } } } } } XML in .NE XML in .NE T T - - Editor Editor : : Đo Đo à à n Quang n Quang Minh Minh 8 8 Ghi Ghi XML XML Đ Đ ể ể ghi ghi XML, XML, d d ù ù ng ng XmlTextWriter XmlTextWriter . . – – H H à à m m t t ạ ạ o o v v ớ ớ i i tên tên file file c c ầ ầ n n ghi ghi . . – – C C á á c c phương phương th th ứ ứ c c quan quan tr tr ọ ọ ng ng . . WriteStartDocument WriteStartDocument (): (): ghi ghi ph ph ầ ầ n n khai khai b b á á o o version XML. version XML. WriteStartElement(string WriteStartElement(string ): ): ghi ghi th th ẻ ẻ m m ở ở đ đ ầ ầ u u c c ủ ủ a a m m ộ ộ t t n n ú ú t t . . WriteAttributeString(string WriteAttributeString(string , string): , string): ghi ghi thu thu ộ ộ c c t t í í nh nh v v à à gi gi á á tr tr ị ị c c ủ ủ a a n n ó ó . . WriteElementString(string WriteElementString(string , string): , string): ghi ghi m m ộ ộ t t n n ú ú t t , , trong trong đ đ ó ó c c ó ó ch ch ứ ứ a a m m ộ ộ t t gi gi á á tr tr ị ị . . WriteEndElement WriteEndElement (): (): ghi ghi th th ẻ ẻ k k ế ế t t th th ú ú c c c c ủ ủ a a m m ộ ộ t t n n ú ú t t . . XML in .NE XML in .NE T T - - Editor Editor : : Đo Đo à à n Quang n Quang Minh Minh 9 9 Ghi Ghi XML XML public class Sample public class Sample { { public static void Main() public static void Main() { { XmlTextWriter XmlTextWriter writer = new writer = new XmlTextWriter("titles.xml XmlTextWriter("titles.xml ", null); ", null); //Write the root element //Write the root element writer.WriteStartElement("items writer.WriteStartElement("items "); "); //Write sub //Write sub - - elements elements writer.WriteElementString("title writer.WriteElementString("title ", "Unreal Tournament 2003"); ", "Unreal Tournament 2003"); writer.WriteElementString("title writer.WriteElementString("title ", "C&C: Renegade"); ", "C&C: Renegade"); writer.WriteElementString("title writer.WriteElementString("title ", "Dr. Seuss's ABC"); ", "Dr. Seuss's ABC"); // end the root element // end the root element writer.WriteEndElement writer.WriteEndElement (); (); //Write the XML to file and close the writer //Write the XML to file and close the writer writer.Close writer.Close (); (); } } } } XML in .NE XML in .NE T T - - Editor Editor : : Đo Đo à à n Quang n Quang Minh Minh 10 10 DOM DOM trong trong .NET .NET DOM (Document Object Model): DOM (Document Object Model): mô mô h h ì ì nh nh đ đ ố ố i i tư tư ợ ợ ng ng t t à à i i li li ệ ệ u u cho cho ph ph é é p p x x ử ử lý lý XML XML m m ộ ộ t t c c á á ch ch m m ề ề m m d d ẻ ẻ o o – – Kh Kh á á c c v v ớ ớ i i XmlTextReader XmlTextReader , , XmlTextWriter XmlTextWriter ch ch ỉ ỉ cho cho ph ph é é p p đ đ ọ ọ c c v v à à ghi ghi XML XML theo theo ki ki ể ể u u tu tu ầ ầ n n t t ự ự , DOM , DOM cho cho ph ph é é p p truy truy c c ậ ậ p p ng ng ẫ ẫ u u nhiên nhiên v v à à o o t t à à i i li li ệ ệ u u XML. XML. – – C C á á c c l l ớ ớ p p quan quan tr tr ọ ọ ng ng XmlDocument XmlDocument : : cho cho ph ph é é p p x x ử ử lý lý XML XML theo theo DOM DOM XmlNodeList XmlNodeList : : danh danh s s á á ch ch c c á á c c node node trong trong t t à à i i li li ệ ệ u u XML. XML. XmlNode XmlNode : : m m ộ ộ t t node node đơn đơn nh nh ấ ấ t t trong trong t t à à i i li li ệ ệ u u – – XmlDocument XmlDocument cho cho ph ph é é p p thêm thêm node node m m ớ ớ i i , , n n ố ố i i node node v v à à o o đuôi đuôi t t à à i i li li ệ ệ u u , , xo xo á á node node kh kh ỏ ỏ i i t t à à i i li li ệ ệ u u . . XML in .NE XML in .NE T T - - Editor Editor : : Đo Đo à à n Quang n Quang Minh Minh 11 11 DOM DOM trong trong .NET .NET void Sample() void Sample() { { oXmlDoc oXmlDoc = new = new XmlDocument XmlDocument (); (); oXmlDoc.Load(Server.MapPath("xml_XmlDocument.xml oXmlDoc.Load(Server.MapPath("xml_XmlDocument.xml ")); ")); XmlNode XmlNode oNode oNode = = oXmlDoc.DocumentElement oXmlDoc.DocumentElement ; ; Console.WriteLine("Node Console.WriteLine("Node Name: " + Name: " + oNode.Name oNode.Name ); ); XmlNodeList XmlNodeList oNodeList oNodeList = = oNode.SelectNodes oNode.SelectNodes ("/books/category/title"); ("/books/category/title"); Console.WriteLine("NodeList Console.WriteLine("NodeList count=" + count=" + oNodeList.Count oNodeList.Count ); ); for(int for(int x = 0; x < x = 0; x < oNodeList.Count oNodeList.Count ; x++) ; x++) Console.WriteLine("NodeList Console.WriteLine("NodeList Item#" + x + " " + Item#" + x + " " + oNodeList.Item[x].InnerText oNodeList.Item[x].InnerText ); ); } } . d d ù ù ng ng XmlTextWriter XmlTextWriter . . – – H H à à m m t t ạ ạ o o v v ớ ớ i i tên tên file file c c ầ ầ n n ghi ghi . . – – C C á á c c phương phương th th ứ ứ c c quan quan tr tr ọ ọ ng ng . . WriteStartDocument WriteStartDocument (): (): ghi ghi ph ph ầ ầ n n khai khai b b á á o o version. , DOM cho cho ph ph é é p p truy truy c c ậ ậ p p ng ng ẫ ẫ u u nhiên nhiên v v à à o o t t à à i i li li ệ ệ u u XML. XML. – – C C á á c c l l ớ ớ p p quan quan tr tr ọ ọ ng ng XmlDocument XmlDocument :. tr tr ọ ọ ng ng XmlDocument XmlDocument : : cho cho ph ph é é p p x x ử ử lý lý XML XML theo theo DOM DOM XmlNodeList XmlNodeList : : danh danh s s á á ch ch c c á á c c node node trong trong t t à à i i li li ệ ệ u u XML. XML. XmlNode XmlNode :

Ngày đăng: 08/07/2014, 09:20

Mục lục

  • C# and .NET Framework Bài 1: C Sharp và kiến trúc .NET. C# cơ bản

  • Mục lục

  • Mối quan hệ giữa C# và .NET

  • The Common Language Runtime

  • Intermediate Language

  • Assemblies

  • .NET Framework Classes

  • Các ứng dụng sử dụng C#

  • Các ứng dụng sử dụng C#

  • C# cơ bản

  • C# cơ bản

  • C# cơ bản

  • Tài liệu tham khảo

  • C# and .NET - Lession 2.pdf

    • C# and .NET Framework Bài 2: Hướng đối tượng trong C#

    • Mục lục

    • Lớp và kế thừa (class & inherit)

    • Lớp và kế thừa (class & inherit)

    • Thuộc tính (property)

    • Lớp Object

    • Giao tiếp (interface)

Tài liệu cùng người dùng

Tài liệu liên quan