Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 XML Parsing in C# Why.NET is worth it…. 2 Introduction C# was designed around the.NET platform for… Similarity to C++ Similarity to C++ Safety and ease.

Similar presentations


Presentation on theme: "1 XML Parsing in C# Why.NET is worth it…. 2 Introduction C# was designed around the.NET platform for… Similarity to C++ Similarity to C++ Safety and ease."— Presentation transcript:

1 1 XML Parsing in C# Why.NET is worth it…

2 2 Introduction C# was designed around the.NET platform for… Similarity to C++ Similarity to C++ Safety and ease of use Safety and ease of use True OOP structure True OOP structure Interoperability with other languages (instantiate a C++ class in C#) Interoperability with other languages (instantiate a C++ class in C#) Allowing existing code to be used (ie – COM objects, DLLs) Allowing existing code to be used (ie – COM objects, DLLs)

3 3 Introduction What languages influenced its creation? Visual Basic (Rapid App Dev capabilities) Visual Basic (Rapid App Dev capabilities) Java (Type-safe Ref Modeling) Java (Type-safe Ref Modeling) C++ (Power inherited from C) C++ (Power inherited from C) Smalltalk (true object-oriented structure) Smalltalk (true object-oriented structure) … along with a number of predecessors

4 4 The.NET Framework Base Class Library containing hundreds of classes Base Class Library containing hundreds of classes Contains underlying object-oriented principles as well as assembly technology Contains underlying object-oriented principles as well as assembly technology All classes are based on one specific class in the BCL All classes are based on one specific class in the BCL

5 5 Basic C# Namespaces used instead of include or import calls Namespaces used instead of include or import calls using System; using System.IO; using System.Xml; This acts as a container for underlying classes, such as Console, XML parsing functions This acts as a container for underlying classes, such as Console, XML parsing functions Console.WriteLine(“It is called Mario Twins”); XmlDocument xmlDoc = new XmlDocument();

6 6 C# Code Structure using System;// Include the namespace public class SampleCode {// Create your first class public static void Main() {// Main declared within the class int x, y; Console.Write(“Enter two numbers: “);// Write text x = Convert.ToInt32(Console.ReadLine()); // Convert input to int y = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(“The sum is “ + Sum(x,y)); // Write text & vars } public static int Sum(int a, int b) { return(a + b); } }// End of class

7 7 C# OOP Structure OOP structure almost identical to C++ in terms of classes, structs OOP structure almost identical to C++ in terms of classes, structs Just like C++, it contains private and public member variables and functions Just like C++, it contains private and public member variables and functions The Main() is placed in one of the classes, and the compiler enters runtime via this function The Main() is placed in one of the classes, and the compiler enters runtime via this function Namespaces can be used to contain classes for organization Namespaces can be used to contain classes for organization

8 8 XML Parsing Features Using the System.Xml namespace, Using the System.Xml namespace, XmlDocument xmlDoc; // Our XML doc to be loaded XmlTextWriter writer;// The stream we’ll used to … write out the results xmlDoc.Load(@”courselist.xml”); // Load the doc … writeFileName = myCourse.getCrsNum() + “.xml”; writer = new XmlTextWriter(writeFileName, null); // Open the … output file writer.WriteStartElement(“courseinfo”); // Write an XML element writer.WriteElementString(“coursename”, // Write the inner myCourse.courseName); // elements … writer.WriteEndElement(); // Close the element (well-formed)

9 9 More XML Parsing Goodness Throw in a few more namespaces for an even better time… Throw in a few more namespaces for an even better time… System.Xml.XPath allows a DOM tree to be parsed node by node, and even searched by level of indentation System.Xml.XPath allows a DOM tree to be parsed node by node, and even searched by level of indentation System.Xml.Xsl allows a program to load an.xsl for styling, and then output everything to.xml System.Xml.Xsl allows a program to load an.xsl for styling, and then output everything to.xml

10 10 More XML Parsing Features With the System.Xml.XPath namespace tacked on, With the System.Xml.XPath namespace tacked on, XmlNodeList xmlNodes; xmlDoc.Load(@”courselist.xml”); xmlNodes = xmlDoc.SelectNodes(“//courselist/course”); // Select all …individual class elements if(xmlNodes.Count > 0) foreach(XmlNode myNode in xmlNodes) { if(myNode.Type == “Element”) { myName = myNode[“coursename”].InnerText; myNum = myNode[“coursenum”].InnerText; myInstr = myNode[“instructor”].InnerText; Course temp = new Course(myName, …); …

11 11 Questions?


Download ppt "1 XML Parsing in C# Why.NET is worth it…. 2 Introduction C# was designed around the.NET platform for… Similarity to C++ Similarity to C++ Safety and ease."

Similar presentations


Ads by Google