csharp presentation
Embed Size (px)
TRANSCRIPT
-
8/12/2019 Csharp Presentation
1/11
1
XML Parsing in C#Why .NET is worth it
-
8/12/2019 Csharp Presentation
2/11
2
Introduction
C# was designed around the .NETplatform for Similarity to C++Safety and ease of useTrue OOP structureInteroperability with other languages(instantiate a C++ class in C#)
Allowing existing code to be used (ie COM objects, DLLs)
-
8/12/2019 Csharp Presentation
3/11
3
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
-
8/12/2019 Csharp Presentation
4/11
4
The .NET Framework
Base Class Library containing hundredsof classesContains underlying object-orientedprinciples as well as assemblytechnology
All classes are based on one specificclass in the BCL
-
8/12/2019 Csharp Presentation
5/11
5
Basic C#
Namespaces used instead of include orimport calls
using System;
using System.IO;using System.Xml;
This acts as a container for underlyingclasses, such as Console, XML parsingfunctions
Console.WriteLine(It is called Mario Twins); XmlDocument xmlDoc = new XmlDocument();
-
8/12/2019 Csharp Presentation
6/11
6
C# Code Structure
using System; // Include the namespace
public class SampleCode { // Create your first classpublic static void Main() { // Main declared within the class
int x, y;Console.Write(Enter two numbers: ); // Write textx = Convert.ToInt32(Console.ReadLine()); // Convert input to inty = 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
-
8/12/2019 Csharp Presentation
7/11
7
C# OOP Structure
OOP structure almost identical to C++ interms of classes, structsJust like C++, it contains private andpublic member variables and functionsThe Main() is placed in one of the classes,and the compiler enters runtime via thisfunctionNamespaces can be used to containclasses for organization
-
8/12/2019 Csharp Presentation
8/11
8
XML Parsing Features
Using the System.Xml namespace,XmlDocument xmlDoc; // Our XML doc to be loadedXmlTextWriter writer; // The stream well used to write out the resultsxmlDoc.Load(@courselist.xml); // Load the doc
writeFileName = myCourse.getCrsNum() + .xml; writer = new XmlTextWriter(writeFileName, null); // Open the output filewriter.WriteStartElement(courseinfo); // Write an XML elementwriter.WriteElementString(coursename, // Write the inner
myCourse.courseName); // elements writer.WriteEndElement(); // Close the element (well-formed)
-
8/12/2019 Csharp Presentation
9/11
9
More XML Parsing Goodness
Throw in a few more namespaces for aneven better time System.Xml.XPath allows a DOM tree to beparsed node by node, and evensearched by level of indentationSystem.Xml.Xsl allows a program to loadan .xsl for styling, and then outputeverything to .xml
-
8/12/2019 Csharp Presentation
10/11
10
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 elementsif(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, );
-
8/12/2019 Csharp Presentation
11/11
11
Questions?