XML and JSON 1 XML a very brief
XML and JSON 1
XML, a very brief introduction • XML = e. Xtensible Markup Language • Language to describe data to be exchanged on the web • Example XML document • 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 <school> <teacher> <name>Anders</name> <city>Roskilde</name> </teacher> <name>Michael</name> <city>Valby</name> </teacher> </school> • Nested elements • An element has exactly 1 super-element • Except the root element, which has 0 superelements XML and JSON 2
Structure in XML documents • Well-formed • Valid • All start-tags must have a matching end-tag • Well-formed • Conforms to an XML schema • <teacher>…</teacher> • A set of “grammar rules” • Specifies which tags are allowed, etc. • Tags must be properly nested • <school> • <teacher>…</teacher> • </school> • Only 1 outer tag • Called the root element XML and JSON 3
XML serialization • A C# object can be serialized into an XML document. • The state of the object is made into an XML document • Class Xml. Serializer • Example: Xml. Serialization • 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: Xml. Serialization XML and JSON 4
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. w 3 schools. com/xml_xpath. asp • xpath. Examples, using class Xml. Document XML and JSON 5
JSON • JSON = Java. Script Object Notation • Language for storing and exchanging data • Structure • { object } • “name” : “value” • “name” : [“value 1”, “value 2”] • Examples • http: //www. w 3 schools. com/json_syntax. asp XML and JSON 6
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 Newton. Soft • http: //www. newtonsoft. com/json • Nu. Get this package into your Visual Studio solution • Examples json. Serialization XML and JSON 7
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 XML and JSON 8
- Slides: 8