Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

How to get the nodes from a certain parent in XML

$
0
0

Hello

How do you get the child nodes of a parent node with a certain name using the SelectNodes function?

I want to compare two files. I have XML files like this:

<?xml version="1.0"?><vobject language="nl"><nspace name="One"><cner name="Mutation"><ctrl name="General"><txt>Algemeen</txt></ctrl></cner></nspace></vobject>

But then there are more nspace nodes, with more cner childnodes, and more ctrl nodes, etc. There is always only one txt.

I want to compare two XML files for content.

If a nspace, or cnor or ctrl is the missing in the other file. I want to have a report of that.

If the whole path nspace->cnor->ctrl is the same. But the txt content is not, I want to have a report of that. So far I have come up with this:

            XmlDocument xmlorgdoc1 = new XmlDocument();
            xmlorgdoc1.Load(textOriginalXML.Text);

            XmlDocument xmlsyscondoc2 = new XmlDocument();
            xmlsyscondoc2.Load(textSavedFromSyscon.Text);

            XmlNodeList containersOrg = xmlorgdoc1.DocumentElement.SelectNodes("//nspace");
            XmlNodeList containersSyscon = xmlsyscondoc2.DocumentElement.SelectNodes("//nspace");

            foreach (XmlNode n1 in containersOrg)
            {
                string nameattribute1 = n1.Attributes["name"].Value;
                if (GetInOtherList(nameattribute1, containersSyscon))
                {
                    foundNamespaces.Add(nameattribute1);
                }
                else
                {
                    notfoundNamespacesInSys.Add(nameattribute1);
                }
            }
            foreach (XmlNode n2 in containersSyscon)
            {
                string nameattribute2 = n2.Attributes["name"].Value;
                bool found = (foundNamespaces.Find(item => item == nameattribute2) != null);
                if (!found)
                {
                    notfoundNamespacesInOrg.Add(n2.Name);
                }
            }

But now for al nspace nodes in the two files, that are found in both files, I want to compare the child cner nodes in more of less the same way. Then with the cners in that nspace node that are the same, I want to compare the ctrls, etcetera. And in the end I want to compere the contents of the txt.


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>