Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 MSCF/CMU1.NET and XML

2 2 From Objects to XML using 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; } }

3 MSCF/CMU3 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;

4 MSCF/CMU4 XmlSerializer xs = new XmlSerializer(typeof(Node)); Stream s = File.OpenWrite("Node.xml"); xs.Serialize(s,n); s.Close(); }

5 MSCF/CMU5 Node.xml 100 3

6 MSCF/CMU6 Controlling Serialization using System; using System.Xml.Serialization; using System.IO; namespace XmlTest { [XmlRoot("TreeNode")] public class Node { private double stockPrice; private double optionPrice;

7 MSCF/CMU7 [XmlElement("sp")] public double StockPrice { get { return stockPrice; } set { stockPrice = value; } } [XmlElement("op")] public double OptionPrice { get { return optionPrice; } set { optionPrice = value; } }

8 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(); }

9 MSCF/CMU9 Node.xml <TreeNode xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 100 3 e>3

10 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); }

11 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; }

12 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); }

13 MSCF/CMU13 ACoolNodeService.asmx 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)

14 MSCF/CMU14 Response in a browser 100 3

15 MSCF/CMU15 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

16 MSCF/CMU16 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>ClientMain 3 100

17 MSCF/CMU17 Basic XML Processing // XmlTextReader Demo using System.Xml; using System.IO; using System; class XmlDemo { static void Main() { String xml = " 100 "; XmlTextReader xmlTextReader = new XmlTextReader( new StringReader(xml));

18 MSCF/CMU18 // 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;

19 MSCF/CMU19 // 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 student score 100

20 MSCF/CMU20 Reading a file FixedFloatSwap.xml Bank of London 100 5 3 6

21 MSCF/CMU21 // Reading any XML file using System.Xml; using System.IO; using System; class XmlDemo { static void Main() { XmlTextReader reader = new XmlTextReader("FixedFloatSwap.xml");

22 MSCF/CMU22 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;

23 MSCF/CMU23 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; }

24 24 D:\McCarthy\www\46-690\examples\XmlProcessing>XmlURLDemo XmlDeclaration Whitespace Element Whitespace Element Text Whitespace Element Attributes found Text Whitespace Element Text Whitespace Element Text Whitespace Element Text Whitespace

25 MSCF/CMU25 Reading from the net Suppose the FixedFloatSwap.xml is found At http://localhost/FpML/FixedFloatSwap.xmlhttp://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.

26 MSCF/CMU26 // 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;

27 MSCF/CMU27 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;

28 MSCF/CMU28 case XmlNodeType.Text: Console.WriteLine("Text"); break; case XmlNodeType.Whitespace: Console.WriteLine("Whitespace"); break; }

29 MSCF/CMU29 D:\McCarthy\www\46-690\examples\XmlProcessing>XmlURLDemo XmlDeclaration Whitespace Element Whitespace Element Text Whitespace Element Attributes found Text Whitespace Element Text Whitespace Element Text Whitespace Element Text Whitespace


Download ppt "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."

Similar presentations


Ads by Google