XML and JSON 1. XML, a very brief introduction XML = eXtensible Markup Language Language to describe...
-
Upload
ruby-mosley -
Category
Documents
-
view
215 -
download
1
Transcript of XML and JSON 1. XML, a very brief introduction XML = eXtensible Markup Language Language to describe...
XML and JSON 1
XML and JSON
XML and JSON 2
XML, a very brief introduction• XML = eXtensible Markup Language• Language to describe data to be
exchanged on the web• And many other things• Program exchange XML documents• XML documents are processed by programs
• Mark up language• XML tags structure information
• Element• <tag> …. </tag>• An element may have many sub-elements
• Nested elements• An element has exactly 1 super-element
• Except the root element, which has 0 super-elements
• Example XML document<school> <teacher> <name>Anders</name> <city>Roskilde</name> </teacher> <teacher> <name>Michael</name> <city>Valby</name> </teacher></school>
XML and JSON 3
Structure in XML documents
• Well-formed• All start-tags must have a
matching end-tag• <teacher>…</teacher>
• Tags must be properly nested• <school>• <teacher>…</teacher>• </school>
• Only 1 outer tag• Called the root element
• Valid• Well-formed• Conforms to an XML schema
• A set of “grammar rules”• Specifies which tags are allowed, etc.
XML and JSON 4
XML serialization
• A C# object can be serialized into an XML document.• The state of the object is made into an XML document• Class XmlSerializer• Example: XmlSerialization
• An XML object can be de-serialized into a C# object• Create new object using default constructor• Use set part of properties to set the state of the object• Example: XmlSerialization
XML and JSON 5
XPath
• XPath is a language for finding information in an XML document.• With Xpath you can extract the information you need from an XML document
• Examples• http://www.w3schools.com/xml/xml_xpath.asp• xpathExamples, using class XmlDocument
XML and JSON 6
JSON
• JSON = JavaScript Object Notation• Language for storing and exchanging data• Structure• { object }• “name” : “value”• “name” : [“value1”, “value2”]
• Examples• http://www.w3schools.com/json/json_syntax.asp
XML and JSON 7
XML serialization
• A C# object can be serialized into a JSON string• The state on the object is serialized
• A JSON string can be de-serialized into a C# object• Create new object using default constructor• Use set part of properties to set the state of the object
• API• C# has some …• Most C# programmers use JSON.NET from NewtonSoft
• http://www.newtonsoft.com/json• NuGet this package into your Visual Studio solution
• Examples jsonSerialization
XML and JSON 8
XML vs. JSON
• JSON is simpler than XML• JSON string are shorter than XML documents• Use less disk / network bandwidth
• XML came first, then JSON