29
MSCF/CMU 1 .NET and XML

MSCF/CMU1.NET and XML. 2 From Objects to XML using System; using System.Xml.Serialization; using System.IO; namespace XmlTest { public class Node { private

  • View
    225

  • Download
    0

Embed Size (px)

Citation preview

MSCF/CMU 1

.NET and XML

MSCF/CMU 2

From Objects to XMLusing System;using System.Xml.Serialization;using System.IO;

namespace XmlTest {

public class Node {

private double stockPrice; private double optionPrice;

public double StockPrice { get { return stockPrice; } set { stockPrice = value; } }

MSCF/CMU 3

public double OptionPrice {

get { return optionPrice; } set { optionPrice = value; } } }

public class XmlMain {

static void Main(string[] args) {

Node n = new Node(); n.StockPrice = 100.0; n.OptionPrice = 3.00;

MSCF/CMU 4

XmlSerializer xs = new XmlSerializer(typeof(Node));

Stream s = File.OpenWrite("Node.xml"); xs.Serialize(s,n); s.Close(); } }}

MSCF/CMU 5

Node.xml

<?xml version="1.0"?><Node xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<StockPrice>100</StockPrice> <OptionPrice>3</OptionPrice></Node>

MSCF/CMU 6

Controlling Serialization

using System;using System.Xml.Serialization;using System.IO;

namespace XmlTest {

[XmlRoot("TreeNode")] public class Node {

private double stockPrice; private double optionPrice;

MSCF/CMU 7

[XmlElement("sp")] public double StockPrice { get { return stockPrice; } set { stockPrice = value; } }

[XmlElement("op")] public double OptionPrice {

get { return optionPrice; } set { optionPrice = value; } } }

MSCF/CMU 8

public class XmlMain {

static void Main(string[] args) {

Node n = new Node(); n.StockPrice = 100.0; n.OptionPrice = 3.00;

XmlSerializer xs = new XmlSerializer(typeof(Node));

Stream s = File.OpenWrite("Node.xml"); xs.Serialize(s,n); s.Close(); } }}

MSCF/CMU 9

Node.xml

<?xml version="1.0"?><TreeNode xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <sp>100</sp> <op>3</op></TreeNode>e>3</OptionPrice></Node>

MSCF/CMU 10

XMLSerializer in Web Services

// NodeService.cs

using System;using System.Web.Services;

namespace XMLWebService{

public class NodeService : System.Web.Services.WebService {

[WebMethod] public Node getNode() {

return new Node(100.0,3.0); } }

MSCF/CMU 11

public class Node {

private double stockPrice; private double optionPrice;

public double StockPrice { get { return stockPrice; } set { stockPrice = value; } }

public double OptionPrice {

get { return optionPrice; } set { optionPrice = value; } }

public Node(double sp, double op) { StockPrice = sp; OptionPrice = op; }

MSCF/CMU 12

// required for web service public Node() { StockPrice = 0.0; OptionPrice = 0.0; } }

public class XmlMain {

public static void Main(string[] args) {

NodeService ns = new NodeService(); Node n = ns.getNode();

Console.WriteLine(n.OptionPrice);

} }}

MSCF/CMU 13

ACoolNodeService.asmx

<%@ WebService Language="C#“ Class="XMLWebService.NodeService" %>

1. Place in a directory above bin

2. In the bin directory place the compiled .dll file NodeService.dll

3. Run IIS and choose default web site

4. Action/New/Virtual Directory and set an alias to the directory containing the .asmx file (above bin)

MSCF/CMU 14

Response in a browser

<?xml version="1.0" encoding="utf-8"?>

<Node xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">

<StockPrice>100</StockPrice>

<OptionPrice>3</OptionPrice>

</Node>

MSCF/CMU 15

Client Side

• Get the wsdl with a browser

• Run wsdl.exe

• Compile output of wsdl to a .dll

• Compile client and reference the .dll

• Run the client

MSCF/CMU 16

using System;

public class ClientMain {

public static void Main() {

NodeService ns = new NodeService();

Node n = ns.getNode();

Console.WriteLine(n.OptionPrice + " " + n.StockPrice);

}}

..\46-690\examples\XMLSerialization\webservices\client>ClientMain3 100

MSCF/CMU 17

Basic XML Processing// XmlTextReader Demo

using System.Xml;using System.IO;using System;

class XmlDemo {

static void Main() {

String xml = "<student><score>100</score></student>";

XmlTextReader xmlTextReader = new XmlTextReader( new StringReader(xml));

MSCF/CMU 18

// build an empty DOM tree XmlDocument doc = new XmlDocument();

// load the tree from a string doc.Load(xmlTextReader);

// get the root of the xml XmlElement root = doc.DocumentElement;

// write its value Console.WriteLine(root.Name);

// point at the child element XmlElement score = (XmlElement)root.FirstChild;

MSCF/CMU 19

// write the element name Console.WriteLine(score.Name);

// point to its child XmlText value = (XmlText) score.FirstChild;

// look at the contents of the text node Console.WriteLine(value.Value);

}}

XmlDemo.exe

studentscore100

MSCF/CMU 20

Reading a file

FixedFloatSwap.xml

<?xml version="1.0" encoding="UTF-8"?><FixedFloatSwap> <Bank>Bank of London</Bank> <Notional currency = "pounds">100</Notional> <Fixed_Rate>5</Fixed_Rate> <NumYears>3</NumYears> <NumPayments>6</NumPayments> </FixedFloatSwap>

MSCF/CMU 21

// Reading any XML file

using System.Xml;using System.IO;using System;

class XmlDemo {

static void Main() {

XmlTextReader reader = new XmlTextReader("FixedFloatSwap.xml");

MSCF/CMU 22

while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.XmlDeclaration: Console.WriteLine("XmlDeclaration"); break; case XmlNodeType.ProcessingInstruction: Console.WriteLine("ProcessingInstruction"); break; case XmlNodeType.DocumentType: Console.WriteLine("DocumentType"); break; case XmlNodeType.Comment: Console.WriteLine("Comment"); break;

MSCF/CMU 23

case XmlNodeType.Element: Console.WriteLine("Element"); if (reader.HasAttributes) Console.WriteLine("Attributes found"); break; case XmlNodeType.Text: Console.WriteLine("Text"); break; case XmlNodeType.Whitespace: Console.WriteLine("Whitespace"); break; } }

}}

MSCF/CMU 24

D:\McCarthy\www\46-690\examples\XmlProcessing>XmlURLDemoXmlDeclarationWhitespaceElementWhitespaceElementTextWhitespaceElementAttributes foundTextWhitespaceElementTextWhitespaceElementTextWhitespaceElementTextWhitespace

MSCF/CMU 25

Reading from the net

• Suppose the FixedFloatSwap.xml is found At http://localhost/FpML/FixedFloatSwap.xml• To do that, simply place the FixedFloatSwap.xml

file in a directory and create a virtual directory

in IIS. This directory would have the alias FpML.

MSCF/CMU 26

// Reading any XML file from a URL

using System.Xml;using System.IO;using System;

class XmlDemo {

static void Main() {

XmlTextReader reader = new XmlTextReader("http://localhost/FpML/FixedFloatSwap.xml");

while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.XmlDeclaration: Console.WriteLine("XmlDeclaration"); break;

MSCF/CMU 27

case XmlNodeType.ProcessingInstruction: Console.WriteLine("ProcessingInstruction"); break; case XmlNodeType.DocumentType: Console.WriteLine("DocumentType"); break; case XmlNodeType.Comment: Console.WriteLine("Comment"); break; case XmlNodeType.Element: Console.WriteLine("Element"); if (reader.HasAttributes) Console.WriteLine("Attributes found"); break;

MSCF/CMU 28

case XmlNodeType.Text: Console.WriteLine("Text"); break; case XmlNodeType.Whitespace: Console.WriteLine("Whitespace"); break; } }

}}

MSCF/CMU 29

D:\McCarthy\www\46-690\examples\XmlProcessing>XmlURLDemoXmlDeclarationWhitespaceElementWhitespaceElementTextWhitespaceElementAttributes foundTextWhitespaceElementTextWhitespaceElementTextWhitespaceElementTextWhitespace