Click here to Skip to main content
15,884,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to get the nodes in the below Xml file?
XML
<?xml version="1.0" encoding="utf-8" ?>
- <xd:xmldiff version="1.0" srcDocHash="4685481238288745685" options="IgnoreChildOrder IgnoreNamespaces IgnorePI IgnorePrefixes" fragments="no" xmlns:xd="http://schemas.microsoft.com/xmltools/2002/xmldiff">
- <xd:node match="1">
- <xd:add>
  <Age>12</Age>
  <Phone>12345678</Phone>
  <Email>as@u.com</Email>
  <address>qwqw</address>
  </xd:add>
  </xd:node>
  </xd:xmldiff>


I want to fetch the nodes Age/Phone/Email/address:

C#
string childnode = string.Empty;
XmlDocument docXml = new XmlDocument();
docXml.Load(diffXml);

var nsmgr = new XmlNamespaceManager(docXml.NameTable);
nsmgr.AddNamespace("xd", "http://schemas.microsoft.com/xmltools/2002/xmldiff");

var nl = docXml.SelectNodes("//xd:add/@href", nsmgr);
//var nl = docXml.SelectNodes("//xd:add", nsmgr);

// Get a list of all player nodes

// Define a single node
XmlNode node;

// Get the root Xml element
XmlElement root = docXml.DocumentElement;

for (int i = 0; i < root.ChildNodes.Count; i++)
{
    if (string.IsNullOrEmpty(childnode) == true)
    {
        childnode = root.Name;
    }
    else
    {
        childnode += "," + root.Name;
    }
}
Posted
Updated 1-Oct-15 0:54am
v2

1 solution

Try it with XmlNode.SelectSingleNode Method :

C#
XmlDocument docXml = new XmlDocument();
docXml.Load(diffXml);
 
var nsmgr = new XmlNamespaceManager(docXml.NameTable);
nsmgr.AddNamespace("xd", "http://schemas.microsoft.com/xmltools/2002/xmldiff");
XmlElement root = docXml.DocumentElement;

XmlNode node = root.SelectSingleNode("Age", nsmgr);
Console.WriteLine(node.InnerXml);

XmlNode node = root.SelectSingleNode("Phone", nsmgr);
Console.WriteLine(node.InnerXml);

// ... and so long ...
 
Share this answer
 
Comments
Maciej Los 1-Oct-15 8:23am    
5ed!
Leo Chapiro 1-Oct-15 8:39am    
Thank you! :)
Kandiya 5-Oct-15 10:13am    
How to compare two XML files and fetch missing nodes using c#?
Tried below code but not working .
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