Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have a xml like this

<screen>
<area id="1"></area>
<area>rajni</area>
<area>xyz</area>


Now,I want to extract node of area with attribute id only.
Posted
Updated 7-Jan-13 18:03pm
v2
Comments
Sergey Alexandrovich Kryukov 7-Jan-13 23:17pm    
Programming language? Platform? Tag all what's applied.
—SA

Here[^] is a guide to XPath.

Here[^] are some reference cards, worth printing out.

Something like "/area[@id='1']", at a guess.
 
Share this answer
 
Comments
Rajni from delhi 8-Jan-13 0:27am    
I want a filter like - to extract node which is having an attribute id. so
only this below node should come.As other area nodes doesn't have an id attribute.
<area id="1"></area>
Christian Graus 8-Jan-13 1:09am    
You could use wildcards to match any attribute at all
I could have given you a direct solution but would prefer you to learn about it such that you can resolve similar issues by yourself in future. Go through this article that details everything and how you can use XPATH to get desired nodes and attributes: Manipulate XML data with XPath and XmlDocument (C#)[^]
 
Share this answer
 
//you can use LINQ for that
add below namespace
using System.Xml.Linq;
// just add parent tag in you xml like or if ur xml have then fire linq query over that tag
XML
<Test>
  <area id="1">Deverloper</area>
  <area>rajni</area>
  <area>xyz</area>
</Test>

//After that create object of xdocument like this
XDocument xmldoc = XDocument.Load(@"path of ur xml file/or xml in string form");
//then
var result= xmldoc.Element("Test").Elements("area").Single(a => (int?)a.Attribute("id") == 1).Value;
//This will bring your result.

Thanks
 
Share this answer
 

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