Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET XML
Hi,
I have a xml like this
 

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

 
Now,I want to extract node of area with attribute id only.
Posted 7-Jan-13 17:14pm
Edited 7-Jan-13 18:03pm
v2
Comments
Sergey Alexandrovich Kryukov at 7-Jan-13 23:17pm
   
Programming language? Platform? Tag all what's applied.
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Here[^] is a guide to XPath.
 
Here[^] are some reference cards, worth printing out.
 
Something like "/area[@id='1']", at a guess.
  Permalink  
Comments
Rajni from delhi at 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 at 8-Jan-13 1:09am
   
You could use wildcards to match any attribute at all
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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#)[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

//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
<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
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid