j2me .net compact frameworkwiki.cs.vsb.cz/images/7/70/dot_net_compact_framework... · 2008. 8....

48
by Roman Szturc 2006 Department of Computer Science VŠB-Technical University of Ostrava 1 J2ME .NET Compact Framework .NET Compact Framework

Upload: others

Post on 12-Oct-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

1

J2ME .NET Compact Framework

.NET Compact

Framework

Page 2: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

2

J2ME .NET Compact Framework

Microsoft entered market of mobile devices in 1996 with its first embedded operating system, Windows CE.

In 2000, Microsoft announced its .NET initiative, an innovative computing platform planned to simplify application development and deployment in the Internet age. .NET offers improved interoperability features based on open Internet standards. The Microsoft .NET Framework is an environment for building, deploying, and running robust, scalable distributed applications.

It resolves many of the problems faced by the current applications on the Windows platform, such as development, deployment, and administration. In 2002, Microsoft released the final version of the .NET framework.

History.NET Framework

[ .N

ET R

efe

rence

Guid

e,

info

rmit

.com

]

Page 3: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

3

J2ME .NET Compact Framework

The CLR manages memory, thread execution, code execution, code safety verification, compilation, and other system services. These features are intrinsic to the managed code that runs on the CLR. The CLR:

● automatically handles object layout and manages references to objects, releasing them when they are no longer being used;

● uses metadata to locate and load classes, lay out instances in memory, resolve method invocations, generate native code, enforce security, and set run-time context boundaries;

● objects written in different languages can communicate with each other, and their behaviors can be tightly integrated.

Common Language Runtime.NET Framework

[ M

SD

N L

ibra

ry,

msd

n.m

icro

soft

.com

]

Page 4: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

4

J2ME .NET Compact Framework

The CLR enforces code robustness by implementing a strict type-and-code-verification infrastructure called the CTS. The CTS ensures that all managed code is self-describing. The various Microsoft and third-party language compilers generate managed code that conforms to the CTS.

Managed code can consume other managed types and instances, while strictly enforcing type fidelity and type safety.

Note: The CTS is similar to Java Byte Code.

Common Type System.NET Framework

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Page 5: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

5

J2ME .NET Compact Framework

The .NET Framework Base Class Library is a collection of reusable types that tightly integrate with the CLR. The class library is object oriented, providing types from which your own managed code can derive functionality.

The BCL provides classes which encapsulate a number of common functions such as file reading and writing, graphic rendering, database interaction, XML document manipulation, and so forth. The BCL is much larger than other libraries, but has much more functionality in one package.

Base Class Library.NET Framework

[ M

SD

N L

ibra

ry,

msd

n.m

icro

soft

.com

]

Page 6: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

6

J2ME .NET Compact Framework

Architecture

Native

CLR

Managed

Hardware

Host Operating System

Platform Adaptation Layer

Execution Engine

Core Framework

Platform Extension Frameworks

Applications

.NET Framework

Page 7: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

7

J2ME .NET Compact Framework

An application domain is an entity alnalogous to a regural operating system process, except that the application domain is completely under the control of the CLR, rather than under the control of the operating system. Every .NET application runs inside an application domain, and the CLR ensures that all resources used by the application during operation are released when the application ends. A single operating system process hosts an instance of the CLR, and that CLR can manage multiple application domains. Each application domain executes a single application.

Application Domains.NET Framework

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Page 8: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

8

J2ME .NET Compact Framework

As with the full .NET Framework, the unit of execution in the Compact Framework is the assembly. An assembly is a dynamic-link library (.dll) or an executable (.exe), and it is the result of compiling and linking your code. An assembly contains not only MSIL for application code, but also metadata, which is additional information containing instructions to the runtime that tall it how to code must be executed.

The most obvious examlpes of attributes are those defined in the file AssemblyInfo.cs, which is usually automatically generated by IDE.

Assemblies.NET Framework

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Page 9: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

9

J2ME .NET Compact Framework

.NET compiles MSIL code to native code before execution. It doesn't compile the whole assembly, but instead it compiles on a method-by-method basis, only when the method is called. Once compiled, the runtime caches the native code for the method in memory for the duration of program execution, so compilation occurs only once. The garbage collector clears up cached code after the program terminates.

JIT Compilation.NET Framework

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Page 10: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

10

J2ME .NET Compact Framework

JIT Compilation ProcessJIT Compilation .NET Framework

Execute native codeuntil method end

Already JIT compiled?

Executea managed method

Locate “Main”class

Load assemblybase class

Load thecontaining class

Verify and JITcompile method

Methodreferences

others?

Load referencedclass

No

Yes

Yes

No

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Page 11: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

11

J2ME .NET Compact Framework

The .NET Compact Framework is a hardware-independent environment for running programs on resource-constrained computing devices, encompassing PDAs, mobile phones, set-top boxes, automotive computing devices, and custom-designed embedded devices. It provides the following functionalities.

The Compact Framework is not a simple port of the desktop version, however, but a complete rewrite designed to execute managed code on multiple CPU architectures and operating systems.

.NET Compact Framework

[ J.

Mis

chel,

The .

NET R

efe

ren

ce G

uid

e,

info

rmit

.com

]

Page 12: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

12

J2ME .NET Compact Framework

The Compact Framework class libraries are necessarily a subset of those found on the desktop. As a result, some of the core functionality missing from the Compact Framework includes the followings:

● server functionality,● remoting,● reflection emit,● C++ development,● application configuration files,● J# and JSL development.

Unsupported Functionality.NET Compact Framework

[ M

SD

N L

ibra

ry,

msd

n.m

icro

soft

.com

]

Page 13: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

13

J2ME .NET Compact Framework

A .NET event is a kind of callback from one class to one or more others. A class publishes an event, and one or more classes subscribe to that event.

Delegates and Events.NET Compact Framework

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]A class publishing an event also defines a delegate method, which defines method signature of any event handlers for that event.

Page 14: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

14

J2ME .NET Compact Framework

class Time { private int time;

public void Tick() { time++; ... }

public delegate void TimeEventHandler( object sender, TimeEventArgs args); public event TimeEventHandler Change;}

Declaring Delegate and EventDelegates and Events

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

By convention, an event handler always takes two arguments. The first parameter is an object that is source of the event. The second parameter is an object that contains the event arguments.

Page 15: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

15

J2ME .NET Compact Framework

public class TimeEventArgs : EventArgs { private int value;

public TimeEventArgs(int value) { this.value = value; }

public int Value { get { return value; } }}

Event ArgumentsDelegates and Events

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

EventArgs is the base class for classes containing event data.

Page 16: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

16

J2ME .NET Compact Framework

class Time { private int time;

public void Tick() { time++; if (Change != null) { TimeEventArgs args = new TimeEventArgs(time); Change(this, args); } }

Raising an EventDelegates and Events

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

When a class needs to raise its event it must determine first if there are any subscribers to the event. If subsribers exist, it must create an instance of an EventArgs and send out the event to the subsribers.

class Time { private int time;

public void Tick() { time++; if (Change != null) { TimeEventArgs args = new TimeEventArgs(time); Change(this, args); } }

Page 17: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

17

J2ME .NET Compact Framework

Raising an EventDelegates and Events

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

A subsriber class subsribes to an event by adding its delegate to the publisher's event.

public void DoSomething() { Time time = new Time(); time.Change += new Time.TimeEventHandler(handleTimeEvent); time.Tick(); time.Tick();}

protected void HandleTimeEvent(object sender, TimeEventArgs args) { Console.WriteLine("time: {0}" + args.Value.ToString());}

Page 18: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

18

J2ME .NET Compact Framework

Working with StringsDelegates and Events

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

A subsriber class subsribes to an event by adding its delegate to the publisher's event.

public void DoSomething() { Time time = new Time(); time.Change += new Time.TimeEventHandler(handleTimeEvent); time.Tick(); time.Tick();}

protected void HandleTimeEvent(object sender, TimeEventArgs args) { Console.WriteLine("time: {0}" + args.Value.ToString());}

Page 19: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

19

J2ME .NET Compact Framework

The String ClassWorking with Strings

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

As in the full .NET Framework, strings represent sequence of Unicode characters and are immutable. Immutability means that assignment of new value to a string object creates an entirely new string object.

The String class is defined in the System namespace. The string keyword in C# is an alias for the String class.

string one = "Spring";string two = "Summer";string three = "Fall";string four = "Winter";

string sentence = one + ", " + two + ", " + three + ", " + four;

Page 20: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

20

J2ME .NET Compact Framework

The String ClassWorking with Strings

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Although the String class is a reference type, in many ways it behaves like a value type.

string one = "Monday";string two = "Monday";

bool equal = one == two;bool identical = (object)one == (object)two;

string one = "Monday";string two = string.Copy(one);

bool equal = one == two;bool identical = (object)one == (object)two;

The Copy() method creates a new independent copy of a string.

Page 21: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

21

J2ME .NET Compact Framework

The @ PrefixWorking with Strings

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The C# language interprets some escape sequences such as "\n" meaning a newline. To switch interpretation of escape sequence off, string literal must be preceded by the @ character.

string one = "Literally \\n";string two = @"Literally \n";

bool equals = (one == two);

Page 22: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

22

J2ME .NET Compact Framework

The StringBuilder ClassWorking with Strings

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The purpose of the StringBuilder class is to provide a mutable implementation for strings. The StringBuilder class is defined in the System.Text namespace.

StringBuilder days = new StringBuilder();days.Append("Tuesday");days.Append(", Wednesday");days.Insert(0, "Monday, ");

Page 23: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

23

J2ME .NET Compact Framework

The DateTime ClassUsing Dates

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The DateTime structure in the .NET Compact Framework implements dates. A DateTime holds values accurate to one tick(100 nanoseconds). Dates are held relative to a start date of 12:00 midnight, January 1, 1 A.D.

Dates are immutable, which means that any operations to modify a date will actually return a new instance of a date.

DateTime now = new DateTime();// Year, month, day, hour, minute, secondDateTime future = new DateTime(2020, 12, 24, 17, 33, 7);

Page 24: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

24

J2ME .NET Compact Framework

The TimeSpan ClassUsing Dates

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

DateTime holds absolute date and time values. A TimeSpan object, frequently used with dates, holds a relative time or a time interval.

DateTime start = new DateTime(...);...// Hour, minute, secondTimeSpan journey = new TimeSpan(2, 46, 57);DateTime end = start + journey;

Note: DateTime and TimeSpan classes are defined in the System namespace.

Page 25: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

25

J2ME .NET Compact Framework

Using Regular Expressions

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

A regular expression (regex or regexp for short) is a special text string for describing a search pattern.

Note: The .NET Compact Framework requires to add a reference to System.Text.RegularExpressions.dll in project in addition to the required using directive.

*\.txt

^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$

Regular expressions are frequently used to test data validity, for instance e-mail address.

Page 26: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

26

J2ME .NET Compact Framework

Regular Expression MatchesUsing Regular Expressions

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The Regex and Match represents an immutable regular expression and corresponding match, respectively.

Regex regex = new Regex(@"[a-z]*ent\b", RegexOptions.IgnoreCase);Match match = regex.Match("Absent pieces");if (match.Success) ...

Regex regex = new Regex(@"[a-z]*ent\b", RegexOptions.IgnoreCase);MatchCollection matches = regex.Matches("Absent of enlightment");for (Match match in matches) { ... match.ToString();}

Page 27: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

27

J2ME .NET Compact Framework

Working with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Extensible Markup Language, or XML, provides a platform-independent way to describe complex data using hierarchical documents composed of elements and attributes.

<?xml version="1.0" encoding="utf-8" ?><feed xmlns="http://www.w3.org/2005/Atom"> <title>Example.com</title> <link href="http://www.example.com/"/> <updated>2006-04-06T20:30:34+01:00</updated> <author> <name>...</name> </author> <entry> ... </entry></feed>

Page 28: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

28

J2ME .NET Compact Framework

Reading XMLWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The XmlReader class is an abstract class defined in the System.Xml namespace. It provides functionality that allows to read an XML document as a sequence of nodes. The XmlReaderclass supports forward-only reading.

String data = "<?xml ...";XmlTextReader reader = new XmlTextReader(new StringReader(data));while (reader.Read()) { ...reader.NodeType... ...reader.Name... ...reader.Value...}

Page 29: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

29

J2ME .NET Compact Framework

Reading XMLWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Nodes have a parent-child relationship and the sequence in which the Element and EndElement nodes appear reflects the nested nature of an XML document.

NodeType: Element Name: feed Value:NodeType: Element Name: title Value:NodeType: Text Name: Value: Example.comNodeType: EndElement Name: title Value:

Note: An EndElement node tells when the end element tag has been read, except in the case of empty elements, where the only an Element node would be read. Use the IsEmptyElement property to detect empty element programmatically.

Page 30: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

30

J2ME .NET Compact Framework

Reading XML AttributesWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Attributes are not read by the Read() method; they are not considered children of their parent element node. To process attribute nodes, the reader must be moved to the attributes and then back to the element.

...if (reader.AttributeCount > 0) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); ...reader.Name... ...reader.Value... }

reader.MoveToElement();}

Page 31: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

31

J2ME .NET Compact Framework

Reading XML AttributesWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

An alternative way to retrieve an attribute value is to use the GetAttribute() method when positioned on the Element node that contains the required attribute.

...if (reader.AttributeCount > 0) { for (int i = 0; i < reader.AttributeCount; i++) { String value = reader.GetAttribute(i); //There is no GetAttributeName method. }}

Page 32: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

32

J2ME .NET Compact Framework

Writing XMLWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The XmlTextWriter creates and persists XML documents. The advanteges of using and XmlTextWriter over performing output manually using Stream.Write calls or similar are threefold:

● XmlTextWriter will write syntacticaly correct XML output;● XmlTextWriter has powerful support for namespaces,

avoiding the need for the user to maintain namespace stacks in a complex document;

● XmlTextWriter will automatically translate any special characters in text to entities, for example transformingx < y to x &lt; y.

Page 33: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

33

J2ME .NET Compact Framework

Writing XMLWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

XmlTextWriter writer = new XmlTextWriter(stream);writer.Formatting = Formatting.Indented;writer.WriteStartDocument();

writer.WriteStartElement("link");writer.WriteAttributeString("href", url.ToString());writer.WriteEndElement();

writer.WriteStartElement("title");writer.WriteString(title);writer.WriteEndElement();

writer.WriteElementString("name", name);

writer.WriteEndDocument();

Page 34: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

34

J2ME .NET Compact Framework

The XmlDocument ClassWorking with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The XmlDocument class builds an in-memory tree view of the document that can be navigated in any direction. The XmlDocument class can be used to both read and write XML, making it possible to read, create or modify an XML document using the same object.

XmlDocument xml = new XmlDocument();xml.Load(stream);

XmlNode root = xml.DocumentElement;XmlNodeList list = root.ChildNodes;foreach (XmlNode node in list) { ...node.Name; ...node.Value;}

Page 35: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

35

J2ME .NET Compact Framework

Accessing AttributesXmlDocument Working with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

As when using XmlReader, it is not possible to access attributes simply by navigating from parent to child. The XmlNode class provides an Attributes property that makes it possible to access an element's attributes.

XmlElement element = (XmlElement)node;

if (element.HasAttributes) ...

foreach (Attribute attribute in element.Attributes) { ...attribute.Name; ...attribute.Value;}

Note: The XmlElement class has a number of additional properties and methods that make it easier to access attribute data.

Page 36: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

36

J2ME .NET Compact Framework

Accessing Other Types of NodesXmlDocument Working with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Each XmlNode has an XmlNodeType enumeration value. The NodeType property determines its value, which indicates the type of node.

XmlNode node = ...;XmlNodeType type = node.NodeType;

if (type == XmlNodeType.Element) ...else if (type == XmlNodeType.Attribute) ...else if (type == XmlNodeType.Comment) ...

Note: The XmlNodeType enumeration defines 16 items of node types. Common properties, such as Name or Value, are not defined for some of them.

Page 37: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

37

J2ME .NET Compact Framework

Searching an XML DocumentXmlDocument Working with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The full .NET Framework provides a variety of methods for searching an XML document by name or ID. Due to restrictions .NET Compact Framework supports the only method for searching: GetElementsByTagName().

XmlDocument document = ...;XmlNodeList products = document.GetElementsByTagName("product");foreach (XmlElement product in products) { ...}

Note: The GetElementsByTagName() returns elements found at any depth, so it is not an efficient method for searching large documents.

Page 38: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

38

J2ME .NET Compact Framework

Modifying an XML DocumentXmlDocument Working with XML

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Much of the power of using the XmlDocument class lies in the ability to modify or create XML document trees.

XmlDocument document = ...;

XmlElement name = document.CreateElement("name");XmlText text = document.CreateTextNode(...);name.AppendChild(text);document.AppendChild(name);

XmlNodeList fonts = document.GetElementsByTagName("font");foreach (XmlElement font in fonts) font.SetAttribute("weight", "bold");

document.Save(output);

Page 39: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

39

J2ME .NET Compact Framework

Input and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The .NET Compact Framework supports I/O operations at two levels: low-level byte access using Stream objects, and character access using StreamReader and StreamWriter objects.

The most important fact concerning the use of a stream is that it reads and writes in the form of bytes. Contrast this with StreamReader and StreamWriter, which read and write characters. If for example an application processes Unicode files, one character is held in 2 bytes.

Note: The most operations performed while manipulating an I/O can throw an IOException.

Page 40: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

40

J2ME .NET Compact Framework

Accessing File ContentsInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The FileStream enables access to file content. Number of parameters can be specified when creating the stream object.

FileStream input = new FileStream(source, FileMode.Open, FileAccess.Read);FileStream output = new FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write);

int ch;while ((ch = input.ReadByte()) != -1) output.WriteByte((byte)ch);

input.Close();output.Close();

Page 41: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

41

J2ME .NET Compact Framework

Accessing File ContentInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Reading and writing byte per byte is very unefficient. A buffer should be used as a parameter of Read() and Write() methods in order to encrease performance.

int count;byte[] buffer = new byte[1024];while ((count = input.Read(buffer, 0, buffer.Length)) > 0) output.Write(buffer, 0, count);

Page 42: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

42

J2ME .NET Compact Framework

Seeking on StreamsInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The FileStream class supports a Seek() method, which changes the position of the read and write pointer within the stream. The current position of the pointer can be determined by the Position property.

long offset;int count;byte[] buffer;...input.Seek(offset, SeekOrigin.Begin);count = input.Read(buffer, 0, buffer.Length);

long position = input.Position;

Page 43: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

43

J2ME .NET Compact Framework

Asynchronous Read and WriteInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

Most operations performed on streams are synchronous, that is the call to perform a read or write operation blocks until the operation completes.

However, in some situations it is desirable to perform asynchronous read or write operations. In an asynchronous operation, the code makes a method call to commence the operation, but control then immediately returns to the caller, which might then continue program execution. The caller might return later to check the result of the operation or, either as a result of callback from the .NET Compact Framework code or, if no callback is specified, at a time of the caller's choosing. tasks.

Note: The current release of the .NET Compact Framework supports asynchronous operations on NetworkStream objects only.

Page 44: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

44

J2ME .NET Compact Framework

Seeking on StreamsAsynchronous Input and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The FileStream class supports a Seek() method, which changes the position of the read and write pointer within the stream. The current position of the pointer can be determined by the Position property.

long offset;int count;byte[] buffer;...input.Seek(offset, SeekOrigin.Begin);count = input.Read(buffer, 0, buffer.Length);

long position = input.Position;

Page 45: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

45

J2ME .NET Compact Framework

Using the MemoryStream ClassInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

A MemoryStream object frequently frees the developer from having to implemnt temporary buffers and from having to implement sizing and position-tracking features.

MemoryStream stream = new MemoryStream();stream.Write(buffer, ...);

StreamWriter writer = new StreamWriter(stream);writer.Write("Hello");

A MemoryStream object is often used as a buffer wrapper. In that case, however, the buffer is not resizable.

byte[] buffer = new byte[size];MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length);

Page 46: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

46

J2ME .NET Compact Framework

Using Readers and WritersInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The StreamReader and StreamWriter classes perform input and output of characters with a stream using a particular encoding to translate the characters into bytes and bytes into characters so that the underlying stream can be read from or written to. These classes offer a simpler, higher level interface than the underlying Stream object, and they can read or write strings.

FileStream stream = new FileStream(name, ...);StreamWriter writer = new StreamWriter(stream, System.Text.Encoding.Unicode);writer.WriteLine("# Example config");

StringBuilder buffer = new StringBuilder();StringWriter writer = new StringWriter(buffer);writer.Write(...);...buffer.ToString();

Page 47: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

47

J2ME .NET Compact Framework

Using the File ClassInput and Output

[ A

. W

igle

y,

S.

Wh

eelw

rig

ht,

.N

ET C

om

pact

Fra

mew

ork

]

The File class provides static members for creating, copying, moving and deleting files. The FileInfo class provides the same features but as instance members of the class.

String fileName = ...if (!File.Exists(fileName)) { FileStream stream = File.Create(fileName); ...}

DateTime creation = File.GetCreationTime(fileName);

Note: The classes Directory and Path are frequently used while working with files. They allow to perform operations on directories and paths, respectively.

Page 48: J2ME .NET Compact Frameworkwiki.cs.vsb.cz/images/7/70/Dot_NET_Compact_Framework... · 2008. 8. 3. · by Roman Szturc 2006 Department of Computer Science VŠB-Technical University

by Roman Szturc2006

Department of Computer ScienceVŠB-Technical University of Ostrava

48

J2ME .NET Compact Framework

Unsupported Functionality.NET Compact Framework

[ h

ttp

://w

ww

.w3.o

rg/2

006/T

alk

s/0

906-M

AP

OS

/#(4

) ]