Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to find a missing child tag by comparing 2 xml files?

I am having a reference Xml. There will be an input xml coming via service. I want to compare this input Xml with the Reference Xml and get the missing child tag if any.
Posted

Hard to tell without seeing which code you have so far.
If I had such a requirement, I would go for creating a XML schema definition (.xsd) file, that I would use for validating the XML file. Much more efficient than comparing two XML files for finding differences.

Here are some reference sites about XSD:
XML Schema - Wikipedia[^]
W3C XML Schema Definition Language (XSD) 1.1[^]
XML Schema Tutorial - w3schools[^]
 
Share this answer
 
Firstly you need create a schema file for your xml file. In schema file how your xml file looks like and what are tags and elements should present. Go through below link how to create schema and rules for xml data.

http://www.w3schools.com/schema/[^]

Then you need to validate service xml data with the schema. Please go through below link how to validate xml data with schema(xsd).

http://www.c-sharpcorner.com/UploadFile/87b416/validating-an-xml-document-programmatically/[^]
 
Share this answer
 
v2
Comments
Kandiya 5-Oct-15 1:57am    
I have a reference xml and an input xml.
In the input xml few child nodes are missing.
I want to fetch those missing child nodes by comparing the reference xml with the Input Xml provided.
[no name] 5-Oct-15 2:05am    
Best solution is to create a schema of reference file, as you don't want to do it then you have to implement your own logic manually. Please go through below link that might help you:

http://stackoverflow.com/questions/18318319/compare-two-xml-files-and-save-differences-to-a-results-file
Kandiya 5-Oct-15 10:15am    
Tried below code but not working .I just want the node missing on comparing. Value difference is not required.
public void CompareXmlTest1(string RefXml, string InputXml)
{

string childnodeCommon = string.Empty;
string childnode = string.Empty;

FileInfo feedList = new FileInfo(RefXml);
FileInfo feedRequest = new FileInfo(InputXml);

// Load the documents
XmlDocument feedListXmlDoc = new XmlDocument();
feedListXmlDoc.Load(RefXml);

// Load the documents
XmlDocument feedRequestXmlDoc = new XmlDocument();
feedRequestXmlDoc.Load(InputXml);

// Define a single node
XmlNode feedListNode;
XmlNode feedRequestNode;

//Get Child Node Names(New)
string feedListNodeName = string.Empty;
string feedRequestNodeName = string.Empty;
//End

// Get the root Xml element
XmlElement feedListRoot = feedListXmlDoc.DocumentElement;
XmlElement feedRequestRoot = feedRequestXmlDoc.DocumentElement;

// Get a list of feeds for the stored list and the request
XmlNodeList feedListXml = feedListRoot.SelectNodes("/Names");
XmlNodeList feedRequestXml = feedRequestRoot.SelectNodes("/Names");



if (feedListRoot.HasChildNodes && feedRequestRoot.HasChildNodes)
{


try
{
// loop through list of Ref Xml Child Nodes
for (int i = 0; i < feedListRoot.ChildNodes.Count; i++)
{
//Get Ref Xml Node
feedListNode = feedListRoot.ChildNodes.Item(i);

//Get Ref Xml Child Node Name
feedListNodeName = feedListRoot.ChildNodes.Item(i).Name.ToString();

//check Input Xml Child Nodes for any missing with the Ref Xml Child Nodes


//Get Input Xml Child Node Name
if (feedRequestRoot.ChildNodes.Item(i) != null)
{
//Get Input Xml Node
feedRequestNode = feedRequestXml.Item(i);

}
//checks to see if child node names is there or not comparing the Input Xml with Ref Xml
else
{
feedListNodeName = feedListRoot.ChildNodes.Item(i).Name.ToString();

if (string.IsNullOrEmpty(childnode) == true)
{
childnode = "Missing Child Node: " + feedListNodeName;

}
else
{
childnode += "," + feedListNodeName;

}

childnodeCommon = childnode + " for the Parent Node " + feedListRoot.Name;


}


}
}
finally
{
//Console.WriteLine("Result file has been written out at " + _resultPath);
}

}
Response.Write(childnodeCommon);
//feedListXmlDoc.Save(_resultPath);
}

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900