Category Archives: Xml

Cinchoo – Nested Xml files

ChoXmlDocument

.NET provides standard XmlDocument object that represents a XML document. But lacks to support multiple sub documents inside a Xml document. Cinchoo addresses this feature through ChoXmlDocument class, where you can specify multiple nested Xml files. It provides an opportunities to break a large Xml document into small manageable sub-documents.

For a sample Xml document as below

<?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>

I’m going to break them down to 3 xml files as below (just for illustrative purpose)

1. XmlTop.xml

<?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>

2. XmlIncludeFile1.xml

<?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" />

3. XmlIncludeFile11.xml

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

How to load and use the document

1. Add reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Xml

using (ChoXmlDocument xmlDocument = new ChoXmlDocument("C:\XmlTop.xml"))
{
    Console.WriteLine(ChoXmlDocument.IndentXMLString(xmlDocument.XmlDocument.OuterXml));
}

Output of the above code would be

<?xml version="1.0" encoding="utf-8"?>
<employees xmlns:cinchoo="http://schemas.cinchoo.com/cinchoo/01/framework">
 <employee name="Top-Tom" state="NY" />
 <!--DO NOT REMOVE - BEGIN INCLUDE C:\XmlIncludeFile1.xml-->
 <employee name="Include1-Mark" state="NM" />
 <!--DO NOT REMOVE - BEGIN INCLUDE C:\XmlIncludeFile11.xml-->
 <employee name="Include11-Tom" state="MD" />
 <!--DO NOT REMOVE - END INCLUDE C:\XmlIncludeFile11.xml-->
 <employee name="Include1-Tom" state="AZ" />
 <!--DO NOT REMOVE - END INCLUDE C:\XmlIncludeFile1.xml-->
 <employee name="Top-Mark" state="NJ" />
</employees>

Or you can use the XmlDocument object exposed as member from ChoXmlDocument object to do other operation on the document.