dotnetspider.com
Login Login    Register      

TutorialsForumCareer DevelopmentResourcesReviewsJobsInterviewCommunitiesProjectsTraining

Subscribe to Subscribers
Talk to Webmaster
Tony John

Facebook
Google+
Twitter
LinkedIn
Online Membersbaskar
More...
Join our online Google+ community for Bloggers, Content Writers and Webmasters




Resources » Code Snippets » General

How to check whether an element is present in the expected range at xpath in XML file?


Posted Date:     Category: General    
Author: Member Level: Gold    Points: 20


There is a sample code to check whether any element comes under expected range given by user in XPath in xml file or not. Code has been developed in C# whereas application is windows based. Scenario has been explained for more clarity.



 


How to check whether an element is present in the expected range at xpath in XML file?
Scenario is as follows
suppose my expected range is Science,Computer,Mathematics and I want to check whether element value comes in that particular range then this code snippet is used.
So in such a case if element is Science,Computer or Mathematics (any one out of these three) then result will be true else it will be false. User has to mention the xpath value which should be present in XML file.
This method checks the elements only at that particular xpath and not whole xml file.
User has to give parameters as xpathvalue , xml file path , expected range value (should be seperated by commas) in the method.
Description -
Create a class library in which put this code snippet and call whenever it is required. Make sure that if the element itself has comma inside it then there is no escape sequence for the same.

public bool Element_Present_In_Range(string element_range_value, string user_defined_xml_file, string user_defined_xpath_value)
{
bool result = false;
XmlDocument user_defined_xml_document = new XmlDocument();
user_defined_xml_document.Load(user_defined_xml_file);
XmlNodeList myNodeList = user_defined_xml_document.SelectNodes(user_defined_xpath_value);
foreach (XmlNode myNode in myNodeList)
{
string[] words = element_range_value.Split(',');
foreach (string word in words)
{
if (myNode.InnerText.Equals(word))
{
result = true;
return result;
}
else
{
result = false;

}
}
}

return result;
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to check whether an element is present in the expected range at xpath in XML file?"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: How to save datatable as a .csv file?
    Previous Resource: How to check whether user defined date format matches with element value’s date format at XPath?
    Return to Resources
    Post New Resource
    Category: General


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Email subscription
  • .NET Jobs
  • .NET Articles
  • .NET Forums
  • Articles Rss Feeds
    Forum Rss Feeds


    About Us    Contact Us    Copyright    Privacy Policy    Terms Of Use    Revenue Sharing sites   Advertise   Talk to Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.