Click here to Skip to main content
Click here to Skip to main content

Cinchoo - Handling nested XML files

, 10 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Way to read the nested xml files

Introduction

Cinchoo is the application framework for .NET. This article reveals one of the feature Cinchoo framework provides for developers, a.k.a reading and storing nested Xml files. .NET provides standard XmlDocument object that represents a Xml document in memory. But lacks in support of multiple sub documents files inside a Xml file. Cinchoo addresses this feature through ChoXmlDocument class, through which you can handle multiple nested Xml files. It provides an opportunities to break a large Xml document into small manageable sub-documents and use them in your applications.

How to use 

Let's assume that below is one of the large xml file you have,

<?xml version="1.0" encoding="utf-8" ?>
<employees>
 <employee name="Top-Tom" state="NY" />
 <employee name="Include1-Mark" state="NM" />
 <employee name="Include11-Tom" state="MD" />
 <employee name="Include1-Tom" state="AZ" />
 <employee name="Top-Mark" state="NJ" />
</employees> 
Here I'm breaking them into multiple small xml files and show you how to read them using Cinchoo framework. 

XmlTop.xml 

This file contains one sub xml document 'XmlIncludeFile1.xml'. The sub xml file is linked using 'include' node with 'path' attribute point to a xml file path. Path can be either absolute or relative file path

<?xml version="1.0" encoding="utf-8" ?>
<employees xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework">
 <employee name="Top-Tom" state="NY" />
 <cinchoo:include path="XmlIncludeFile1.xml" />
 <employee name="Top-Mark" state="NJ" />
</employees>    

Below are the broken down sub XML files:

XmlIncludeFile1.xml 

I break this file further into one more sub XML file (XmlIncludeFile11.xml). Have it linked using 'include' node. 

<?xml version="1.0" encoding="utf-8" ?>
<employee name="Include1-Mark" state="NM" />
<cinchoo:include xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework" path="XmlIncludeFile11.xml" />
<employee name="Include1-Tom" state="AZ" />

XmlIncludeFile11.xml 

<?xml version="1.0" encoding="utf-8" ?>
<employee name="Include11-Tom" state="MD" />

Well, after having the above XML files, here is how we can load and read them in your application.

  1. Create a sample VS.NET (.NET Framework 4.0) Console Application project.   
  2. Add reference to Cinchoo.Core.dll. (Download Cinchoo framework binary here.) 
  3. Use the Cinchoo.Core.Xml namespace.
  4. Below is the code to read the above xml file
using (ChoXmlDocument xmlDocument = new ChoXmlDocument("C:\XmlTop.xml"))
{
    Console.WriteLine(ChoXmlDocument.IndentXMLString(xmlDocument.XmlDocument.OuterXml));
} 
That's all. Now you can access the nodes of XML document elements using ChoXmlDocument.XmlDocument. It is of type System.Xml.XmlDocument. Try for yourself.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Cinchoo

United States United States
No Biography provided

Comments and Discussions

 
QuestionHow about XSD.exe? PinprofessionalCoderPanda27-Feb-14 19:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 10 Dec 2012
Article Copyright 2012 by Cinchoo
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid