Subscribe to Subscribers

Resources » Code Snippets » C# Syntax

How to check whether attribute is present or not in XPath in xml file.


Posted Date:     Category: C# Syntax    
Author: Member Level: Gold    Points: 20


There is a sample code to check whether the attribute is present in XPath in xml file or not. User can set xpath , attribute and xml file dynamically to check this. Code has been developed in C# whereas application is windows based.



How to check whether attribute is present or not in XPath in xml file.
Description -
Create a class library which will check whether the attribute is present in XPath in xml file. For e.g There is one xml file say test.xml in which xpath will be //studentsList/student then suppose user needs to find "firstname" as an attribute which is present in //studentsList/student of test.xml then
user has to give parameters such as firstname i.e. name of the parameter , test.xml i.e. xml file name , //studentsList/student i.e. xpath in xml
Use following code for the same -

public bool Attribute_Present_Or_not(string user_defined_attribute, string user_defined_xml_file, string user_defined_xpath)
{
bool chk_result = false;

try
{
XmlDocument user_xml_document = new XmlDocument();
user_xml_document.Load(user_defined_xml_file);

XmlNodeList user_nodeList = user_xml_document.SelectNodes(user_defined_xpath);
XmlElement root = user_xml_document.DocumentElement;

foreach (XmlNode user_node in user_nodeList)
{
try{
if (user_node.Attributes.GetNamedItem(user_defined_attribute)!=null)

{
chk_result = true;

return chk_result;
}
}
catch (Exception )
{
chk_result = false;

return chk_result;

}

}

}
catch (NullReferenceException)
{
chk_result = false;

return chk_result;
}
catch (XPathException)
{
chk_result = false;

return chk_result;
}

return chk_result;
}





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


Responses to "How to check whether attribute is present or not in 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 get Installed Programs List.
    Previous Resource: How to check whether expected element value is present in XPath in user defined XML file.
    Return to Resources
    Post New Resource
    Category: C# Syntax


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

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2013 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.