1 / 29

.NET and XML

.NET and XML. 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 {

kolya
Download Presentation

.NET and XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. .NET and XML

  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. 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. XmlSerializer xs = new XmlSerializer(typeof(Node)); Stream s = File.OpenWrite("Node.xml"); xs.Serialize(s,n); s.Close(); } } }

  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>

  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;

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

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

  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. 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. // 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. ACoolNodeService.asmx <%@ WebService Language="C#“ Class="XMLWebService.NodeService" %> • Place in a directory above bin • In the bin directory place the compiled .dll file NodeService.dll • Run IIS and choose default web site • Action/New/Virtual Directory and set an alias to the directory containing the .asmx file (above bin)

  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>

  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

  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>ClientMain 3 100

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

  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;

  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 student score 100

  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>

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

  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;

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

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

  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;

  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;

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

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

More Related