XML Parsing in C - PowerPoint PPT Presentation

About This Presentation
Title:

XML Parsing in C

Description:

Interoperability with other languages (instantiate a C class in C#) ... System.Xml.XPath allows a DOM tree to be parsed node by node, and even searched ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 12
Provided by: hwan1
Learn more at: http://www.cs.rpi.edu
Category:
Tags: xml | dom | parsing

less

Transcript and Presenter's Notes

Title: XML Parsing in C


1
XML Parsing in C
  • Why .NET is worth it

2
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)

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

4
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

5
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()

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
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

8
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(_at_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
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

10
More XML Parsing Features
  • With the System.Xml.XPath namespace tacked on,
  • XmlNodeList xmlNodes
  • xmlDoc.Load(_at_courselist.xml)
  • xmlNodes xmlDoc.SelectNodes(//courselist/cours
    e) // Select all
  • individual class elements
  • if(xmlNodes.Count gt 0)
  • foreach(XmlNode myNode in xmlNodes)
  • if(myNode.Type Element)
  • myName myNodecoursename.InnerText
  • myNum myNodecoursenum.InnerText
  • myInstr myNodeinstructor.InnerText
  • Course temp new Course(myName, )

11
Questions?
Write a Comment
User Comments (0)
About PowerShow.com