Open an Xml File and Read in C#
XML file is widely used format to shop and transport data over internet. Parsing XML file is a very basic programming requirement. Here we'll see how to parse and impress the content of an XML file in C programming language.
XML File Format
Before jumping into the code, nosotros should understand basic format of an XML file. Nosotros'll this XML file as an example.
<?xml version="1.0"?> <catalog> <book id="bk101"> <writer>Gambardella, Matthew</writer> <championship>XML Developer'due south Guide</title> <genre>Computer</genre> <price>44.95</cost> <publish_date>2000-10-01</publish_date> <description>An in-depth await at creating applications with XML.</clarification> </book> <book id="bk102"> <author>Ralls, Kim</author> <championship>Midnight Pelting</title> <genre>Fantasy</genre> <price>5.95</toll> <publish_date>2000-12-16</publish_date> <description>A erstwhile architect battles corporate zombies, an evil sorceress, and her ain childhood to become queen of the world.</description> </volume> <book id="bk103"> <author>Corets, Eva</author> <title>Maeve Ascendant</title> <genre>Fantasy</genre> <price>5.95</price> <publish_date>2000-11-17</publish_date> <description>After the collapse of a nanotechnology society in England, the immature survivors lay the foundation for a new social club.</description> </book> </catalog>
XML is a markup language like HTML. Different HTML, the tags are non predefined. Any cord can exist used as XML tag. That's why information technology is called eXtensible Karkup Language. Hither are a few XML construct you lot should be enlightened of.
Tag: tag is most of import XML markup construct that starts with ' < ' and ends with ' > '. <itemize> and <volume> are the examples of tag in our case XML file. Information technology could exist of three types, 1) Start Tag such as <catalog>, ii) End Tag such as </itemize> and 3) Empty Tag such as <catalog />. Empty tag is non present in our case.
Element: Element is logical document component of an XML file. It more often than not starts with a first tag and ends with an end tag. Information technology could exist an empty element tag also. The characters between the start tag and cease tag, if any, are call the content of the chemical element. Element tin incorporate markup including other elements which are chosen children. Our example file contains i big element catalog which has few book elements. We can imagine an XML file as a hierarchical tree structure of elements.
Attribute: Attribute is also a markup construct which is basically a proper noun-value pair. It exists inside a start or empty element tag. In our XML file ' id ' is an case of an attribute in <book id="bk101″> tag.
C Programme to Parse and Print XML File
Standard C library does not provide XML parser. I used libxml2. parser. Then you lot accept to explicitly install the libxml2 evolution library. If you don't have it installed already, run the following control to install it.
For Redhat based Linux:
yum install libxml2-devel
And for Debian based Linux.
apt-get install libxml2-dev
The Program
#include <stdio.h> #include <libxml/parser.h> /*gcc `xml2-config --cflags --libs` test.c*/ int is_leaf(xmlNode * node) { xmlNode * child = node->children; while(kid) { if(kid->type == XML_ELEMENT_NODE) render 0; child = child->next; } return 1; } void print_xml(xmlNode * node, int indent_len) { while(node) { if(node->type == XML_ELEMENT_NODE) { printf("%*c%s:%due south\n", indent_len*2, '-', node->proper noun, is_leaf(node)?xmlNodeGetContent(node):xmlGetProp(node, "id")); } print_xml(node->children, indent_len + 1); node = node->next; } } int main(){ xmlDoc *doctor = Nil; xmlNode *root_element = Zero; doc = xmlReadFile("dummy.xml", Zero, 0); if (md == Nothing) { printf("Could not parse the XML file"); } root_element = xmlDocGetRootElement(doc); print_xml(root_element, one); xmlFreeDoc(doc); xmlCleanupParser(); }
This program first reads the XML file using the xmlReadFile() part. The file name is hard-code as 'dummy.xml'. This file needs to be present before running the program. The xmlReadFile() function returns an XML certificate tree. We get the root chemical element of the XML from the document tree using the xmlDocGetRootElement() role.
The root node (element) of the XML tree is passed to the print_xml() role to impress the whole XML content in hierarchical form. This part traverses all siblings of the input node (including the passed node). If a node is of type ELEMENT then information technology prints some information most the node. libxml2 keeps few other type of nodes also equally sibling of the Chemical element type node. That's why we are skipping all node except ELEMENT type node. Tag name is printed. And if the node is a leafage node, then we print content of the node, otherwise, nosotros print the value of "id" attribute.
We are not printing the content of non-leafage nodes considering libxml2 returns content of all nested children as the content of the node. The the content will be lengthy and repeated. Autonomously from press the information of the node, we are also calling the same function print_xml() recursively for the children of the current node. This way all nodes will go printed.
To compile this plan, run this control.
gcc `xml2-config --cflags --libs` exam.c
Here is the output of the program.
-catalog:(null) -book:bk101 -author:Gambardella, Matthew -championship:XML Developer'due south Guide -genre:Computer -price:44.95 -publish_date:2000-ten-01 -description:An in-depth expect at creating applications with XML. -book:bk102 -author:Ralls, Kim -title:Midnight Rain -genre:Fantasy -price:5.95 -publish_date:2000-12-sixteen -description:A sometime architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world. -book:bk103 -writer:Corets, Eva -title:Maeve Ascendant -genre:Fantasy -toll:v.95 -publish_date:2000-eleven-17 -description:After the plummet of a nanotechnology gild in England, the immature survivors lay the foundation for a new society.
fountainmighter42.blogspot.com
Source: https://qnaplus.com/print-xml-file-tree-form-libxml2-c-programming/
0 Response to "Open an Xml File and Read in C#"
Post a Comment