1

Suppose I have the XML file

<?xml version="1.0" ?>
<Tasks>
    <Task>
         <id>1234</id>
         <name>abc</name>
    </Task>

    <Task>
         <id>5678</id>
         <name>xyz</name>
    </Task>
<Tasks>

How can I extract the value of id using the value of name? For example I know the name is abc. How can i extract the value 1234 from the above file?

Phylogenesis
  • 7,775
  • 19
  • 27
  • [You should use XPath](http://stackoverflow.com/questions/9511608/how-to-read-xml-in-c-sharp-using-xpath). – Phylogenesis Mar 10 '15 at 10:04
  • In xpath i can able to search only for eg in the above code tag contain some attribute and value like this.. but the inner tag in plain one , is there any workaround?? – Vishwas Cheluva N Mar 10 '15 at 10:41
  • possible duplicate of [XPath to select element based on childs child value](http://stackoverflow.com/questions/9683054/xpath-to-select-element-based-on-childs-child-value) – Sam Axe Mar 10 '15 at 10:41

1 Answers1

1

Use the following XPath query to get the id element for a Task with the name of abc:

/Tasks/Task[name='abc']/id

Example of this is here.

Phylogenesis
  • 7,775
  • 19
  • 27
  • Thank you so much Phylogenesis, is there any way i can do this using linq,my team want me to use linq:( ?? looking for your help – Vishwas Cheluva N Mar 11 '15 at 07:03
  • Thank you so much again, it worked for me:) one more query , is it possible to write the xml file without deleting its contents in linq??? hope our xml masters help in this regard – Vishwas Cheluva N Mar 11 '15 at 10:18