Csharp Presentation

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 11

XML Parsing in C#

Why .NET is worth it

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

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

The .NET Framework


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

Basic C#

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
Console.WriteLine(It is called Mario Twins); XmlDocument xmlDoc = new XmlDocument();
5

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
6

C# OOP Structure
OOP structure almost identical to C++ in terms of classes, structs 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 Namespaces can be used to contain classes for organization

XML Parsing Features

Using the System.Xml namespace,


XmlDocument xmlDoc; // Our XML doc to be loaded XmlTextWriter writer; // The stream well 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)
8

More XML Parsing Goodness

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.Xsl allows a program to load an .xsl for styling, and then output everything to .xml
9

More XML Parsing Features

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, );
10

Questions?

11

You might also like