XML Processing SoftUni Team Database Applications Technical Trainers Software University http://softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Table of Contents What is XML? LINQ to XML
What is XML?
XML – Example <?xml version="1.0"?> <library name="Developer's Library"> <book> <title>Professional C# 4.0 and .NET 4</title> <author>Christian Nagel</author> <isbn>978-0-470-50225-9</isbn> </book> <title>Teach Yourself XML in 10 Minutes</title> <author>Andrew H. Watt</author> <isbn>978-0-672-32471-0</isbn> </library>
What is XML? XML (eXtensible Markup Language) Universal notation (data format / language) for describing structured data using text with tags The data is stored together with the meta-data about it Used to describe other languages (formats) for data representation
What is XML? (2) XML looks like HTML Text based language, uses tags and attributes Industrial standard, supported by the W3C (www.w3c.org) Independent of the hardware platform, OS and programming languages
XML – Example XML header tag (prolog) Attribute (key / value pair) <?xml version="1.0"?> <library name="Developer's Library"> <book> <title>Professional C# 4.0 and .NET 4</title> <author>Christian Nagel</author> <isbn>978-0-470-50225-9</isbn> </book> <title>Teach Yourself XML in 10 Minutes</title> <author>Andrew H. Watt</author> <isbn>978-0-672-32471-0</isbn> </library> Root (document) element Element Opening tag Closing tag Element value
XML and HTML Similarities between XML and HTML Both are text based notations Both use tags and attributes Differences between XML and HTML HTML is a language, and XML is a syntax for describing other languages (meta-language) HTML describes the formatting of information, XML describes structured information XML requires the documents to be well-formed
Well-Formed XML Documents Means "syntactically correct XML document" All tags should be closed in the correct order of nesting Attributes should always be closed The document should contain only one root element Tag and attribute names retain certain restrictions
Well-Formed XML Documents (2) Example of incorrect XML document Open / close tags do not match Unclosed attribute values Invalid characters, invalid spacing Check for well-formed XML online: http://xmlvalidation.com <xml> <button bug! value="OK name="b1"> <animation source="trailer.avi"> 1 < 2 < 3 </click-button> < / xml >
XML: Benefits Benefits of XML: XML is human readable (unlike binary formats) Store any kind of structured data Data comes with self-describing meta-data Full Unicode support Exchange data between different systems Custom XML-based languages can be designed for certain apps Parsers available for virtually all languages and platforms
When to Use XML? (2) Disadvantages of XML: XML data is bigger (takes more space) than in binary formats More memory consumption, more network traffic, more hard-disk space, more resources Decreased performance CPU consumption: need of parsing / constructing the XML tags XML is not suitable for all kinds of data E.g. graphics, images and video clips
LINQ to XML
LINQ to XML LINQ to XML LINQ to XML classes: Use the power of LINQ to process XML data Easily read, search, write, modify XML documents LINQ to XML classes: XDocument – represents a LINQ-enabled XML document (containing prolog, root element, …)
LINQ to XML Classes LINQ to XML classes: XElement – represents an XML element in LINQ to XML Methods: Parse(…), Save(), RemoveAtributes(…), SetElementValue(…), SetAtributeValue(…), WriteTo(…) XAttribute Name / value attribute pair
LINQ to XML – Searching with LINQ Searching in XML with LINQ is like searching with LINQ in array XDocument xmlDoc = XDocument.Load("../../books.xml"); var books = from book in xmlDoc.Descendants("book") where book.Element("title").Value.Contains("4.0") select new { Title = book.Element("title").Value, Author = book.Element("author").Value }; foreach (var book in books) { Console.WriteLine(book); }
Creating XML with XElement Need to create this XML fragment? <books> <book> <author>Don Box</author> <title>Essential .NET</title> </book> XElement books = new XElement("books", new XElement("book", new XElement("author", "Don Box"), new XElement("title", "ASP.NET") ) );
Additional XML classes to consider: XML Document – old version of Xdocument XmlReader/XmlWriter - huge XML which cannot fit into memory
XML Processing https://softuni.bg/courses/ © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
License This course (slides, examples, demos, videos, homework, etc.) is licensed under the "Creative Commons Attribution- NonCommercial-ShareAlike 4.0 International" license Attribution: this work may contain portions from "Databases" course by Telerik Academy under CC-BY-NC-SA license © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.
Free Trainings @ Software University Software University Foundation – softuni.org Software University – High-Quality Education, Profession and Job for Software Developers softuni.bg Software University @ Facebook facebook.com/SoftwareUniversity Software University @ YouTube youtube.com/SoftwareUniversity Software University Forums – forum.softuni.bg © Software University Foundation – http://softuni.org This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike license.