Generate XmlDocument with XPathList

 28-Mar-2013   NityaPrakash     XML    Comments  0

I spent almost a day to figure out the algorithm to generate the XML from my list of XPaths.  In our structure we were storing only XPath of leaf node. which was pointing to a data in other table.  We had to show this XPath in TreeView Control on Windows.  I am just going to show here, how I created the XML from list of XPaths.

For example I have below list of XPaths stored in database.

Root/Asset/AssetName
Root/Asset/DateOfCreation


Root/Asset/Category


Root/Asset/LeftColumn/section1/header


Root/Asset/LeftColumn/section1/content


Root/Asset/LeftColumn/Section2/header


Root/Asset/LeftColumn/Section2/content


Root/Asset/RightColumn/Statistic/header


Root/Asset/RightColumn/Statistic/Date


Root/Asset/RightColumn/Statistic/Footnote


Root/Disclaimer

After processing these XPah it should generate below xml:

As a XML standard, there can be only one root node, I created root node first to start with.  Below is the complete code.


       static void Main(string[] args)
        {
            List xPathList = new List();
            xPathList.Add("Root/Asset/AssetName");
            xPathList.Add("Root/Asset/DateOfCreation");
            xPathList.Add("Root/Asset/Category");
            xPathList.Add("Root/Asset/LeftColumn/section1/header");
            xPathList.Add("Root/Asset/LeftColumn/section1/content");
            xPathList.Add("Root/Asset/LeftColumn/Section2/header");
            xPathList.Add("Root/Asset/LeftColumn/Section2/content");
            xPathList.Add("Root/Asset/RightColumn/Statistic/header");
            xPathList.Add("Root/Asset/RightColumn/Statistic/Date");
            xPathList.Add("Root/Asset/RightColumn/Statistic/Footnote");
            xPathList.Add("Root/Disclaimer");

            XmlDocument doc = new XmlDocument();
            XmlNode declarationNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            doc.AppendChild(declarationNode);

            XmlNode rootNode = doc.CreateElement("Root");
            doc.AppendChild( rootNode );

            foreach (string xPath in xPathList)
            {
                GetXmlNode(doc, rootNode, xPath);
            }
            
            
        }

        public static XmlNode GetXmlNode(XmlDocument doc, XmlNode parent, string xPath)
        {
            if (xPath.IndexOf("/") < 0)
            {
                XmlNode node = doc.CreateElement(xPath);
                parent.AppendChild(node);

                return node;
            }
            else
            {
                int lastIndex = xPath.LastIndexOf("/");

                XmlNode node = doc.SelectSingleNode(xPath.Substring(0, lastIndex));
                if (node != null)
                {
                    return GetXmlNode(doc, node, xPath.Substring(lastIndex + 1, xPath.Length - lastIndex - 1));
                }
                else
                {
                    XmlNode pNode = GetXmlNode(doc, doc.FirstChild, xPath.Substring(0, lastIndex));
                    return GetXmlNode(doc, pNode, xPath.Substring(lastIndex + 1, xPath.Length - lastIndex - 1));
                }
            }
        }

GetXmlNode method is recursive method and process each part of the XPath.  If XPath doesn�t contains �/� in the path, this means it is leaf node and has to added in parent node passed in the function.  Otherwise this path need further processing.  We removes last node name from the path and search the node in the document.  If find than leaf node added to found node otherwise we have to create parent node first.  So we passed parent path to GetXmlNode method to repeat the logic.  This way we can get the XML. 

Probably this might not be a good approach or solution, but it worked for me.

Happy coding!


Nitya Prakash Sharma has over 10 years of experience in .NET technology. He is currently working as Senior Consultant in industry. He is always keen to learn new things in Technology and eager to apply wherever is possible. He is also has interest in Photography, sketching and painting.

My Blog
Post Comment

COMMENTS