Top Banner
C#: INTRODUCTION FOR DEVELOPERS Neal Stublen [email protected]
21

Neal Stublen [email protected]. How does XMLReader work? XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Jan 02, 2016

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

C#: INTRODUCTION

FOR DEVELOPERS

Neal Stublen

[email protected]

Page 2: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

How does XMLReader work?

XmlReader.Read()Advances to next nodeXmlReader properties access node name,

value, attributes, etc.Returns false if there are no additional

nodes

Page 3: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Reading XML FilesXmlReader r = XmlReader.Create(path, settings);

r.ReadToDescendant(nodeName);

do{ r.ReadStartElement(nodeName); r.ReadElementContentAsString();} while (r.ReadToNextSibling(nodeName));

r.Close();

Page 4: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Code Practice

Create a new project called CustomerImport

Import Customers into a List<> from any .xml file

Fill a TreeView with the Customer data

Page 5: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Review

TreeView XmlReader

Page 6: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ

Page 7: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

var Data Type We can declare a variable as type “var”

instead of using a specific data type

The compiler assigns an implicit data type at compile-time (not run-time)

var value = 0;var array = new string[10];var list = new List<Customer>();

var str = "Hello"; // str is string typestr = 6; // can’t assign int

Page 8: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

What’s LINQ?

SQL-like query expression on any enumerable object type

int[] numbers = new int[100];for (int index = 0; index < 100; ++index){ numbers[index] = index;}

var odds = from number in numbers where number % 2 != 0 select number;

Page 9: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Try it in LINQPad

Duplicate code from previous slide Filter out prime numbers from array

using LINQ query

Page 10: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ Expressionsfrom [type] element in collection

join element2 in collection2on key1 equals key2

where condition // any boolean expression

orderby expression [ascending|descending]

select columnExpressionselect new [type] { name=value, name=value }

Page 11: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ Expressionsfrom cust in customers

join invoice in invoiceson cust.CustomerID equals invoice.CustomerID

where invoice.InvoiceDate < sixtyDaysAgo

orderby invoice.InvoiceTotal descending

select

Page 12: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Try it with CustomerImport Create a List<Customer> object by

looping through SqlDataReader Perform LINQ queries on

List<Customer>

Page 13: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ-TO-SQL

Page 14: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ-to-SQL

Create objects that represent database entities

Use the same LINQ query expressions against a database

Just restricts “where” clause to SQL-compatible operations

Page 15: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

See It In Action

Page 16: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ on Database Objects Add LINQ data objects to a project Drop tables from Server Explorer Use the new DB to query the database

using LINQ Insert, Update, Delete objects,

db.SubmitChanges() Add related items, db.SubmitChanges() Paging: (from…).Skip(5).Take(5)

Page 17: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

LINQ’d Customer-Invoice List

Using LINQ objects, fill a list view with all invoices sorted by CustomerName

Page 18: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

CONTEXT SENSITIVE HELP

Page 19: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

ToolTips and HelpProviders Add ToolTip control Set ToolTip property on each control and/or

the form Hover over control to show tool tip

Add HelpProvider control Set the HelpString on each property and/or

the form Press F1 while the control has focus to show

help string

Page 20: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

TAB CONTROL

Page 21: Neal Stublen nstublen@jccc.edu. How does XMLReader work?  XmlReader.Read() Advances to next node XmlReader properties access node name, value, attributes,

Tab Control

Separate panels to group items Tab across edge changes panel visibility