I have used preceding-sibling and following-sibling to get the unique skill values from the below XML message.
XmlDocument doc = new XmlDocument();
doc.Load(@"{file-uri}");
MemoryStream xmlStream = new MemoryStream();
doc.Save(xmlStream);
xmlStream.Flush();//Adjust this if you want read your data
xmlStream.Position = 0;
//XmlReader reader = XmlReader.Create(doc.OuterXml);
//XPathDocument document = new XPathDocument(reader);
XPathDocument document = new XPathDocument(xmlStream);
XPathNavigator xPathNavigator = document.CreateNavigator();
xPathNavigator.MoveToRoot(); // Move to the root element.
if (xPathNavigator.HasChildren)
{
xPathNavigator.MoveToFirstChild();
}
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("ns0:", "http://www.RVRCompany.com/en/core/employee");
XPathNodeIterator nodIterator = xPathNavigator.Select("//ns0:Employee/ns0:Project/ns0:Skills/ns0:Skill/ns0:SkillName[not(../../../../following-sibling::ns0:Employee/ns0:Project/ns0:Skills/ns0:Skill/ns0:SkillName = .) and not(preceding-sibling::ns0:SkillName=.)]",namespaceManager);
Console.WriteLine("Unique values **********************************************");
while (nodIterator.MoveNext())
{
Console.WriteLine(nodIterator.Current.Value);
}
Console.ReadLine();
Sample XML:
<ns0:UpdateEmployeesRequest xmlns:ns0="http://www.RVRCompany.com/en/core/employee">
<ns0:EmployeeDetails>
<ns0:Employee>
<ns0:EmpID>1</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ1</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>BizTalk</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
<ns0:Employee>
<ns0:EmpID>2</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ2</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>BizTalk</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Java</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
<ns0:Employee>
<ns0:EmpID>3</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ2</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>VB6</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>.Net</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>SQL Server</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>IIS</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Linux</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
<ns0:Employee>
<ns0:EmpID>3</ns0:EmpID>
<ns0:Project>
<ns0:ProjectName>PROJ2</ns0:ProjectName>
<ns0:Skills>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>VB6</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>MS</ns0:SkillType>
<ns0:SkillName>IIS</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Linux</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>Tomcat</ns0:SkillName>
</ns0:Skill>
<ns0:Skill>
<ns0:SkillType>Sun Micros</ns0:SkillType>
<ns0:SkillName>BizTalk</ns0:SkillName>
</ns0:Skill>
</ns0:Skills>
</ns0:Project>
</ns0:Employee>
</ns0:EmployeeDetails>
</ns0:UpdateEmployeesRequest>
No comments:
Post a Comment