Q:

Getting the value from specific tag of a XML file in C#

I'm starting working with XML files and I know little about it.
What I need is read the value from a certain tag of a XML file
<Tag>
<Name>PBAS</Name>
<ID>1</ID>
<Description>Array of sequence characters as edited by user</Description>
<Type>char</Type>
<Elements>163</Elements>
<Size>163</Size>
    <Value>TCCGAACAAGCGGCGAGTGCTGGGCGAGTGGCCGCGGGGCTGTGGTCCTGTGCACGGTGATCGCAAGGGCCCCCGGGGCTTGCTGGTCTTCCCTAACGGTTCGTGGAACCCCCGAAGCTGGGGGCTGGCCAGGGGCTTAAGGAACCCTTCCTAAATTAACTAC</Value>
</Tag>

I need, in the example above, getting the string between the "Value"(the string TCCGA...) of the tag named "PBAS" with "ID 1".
But I need this value just from the tag named PBAS with "ID1", since the XML document has other "values" from other tags named differently.
Thanks a lot!

A:

var yourval = XDocument.Load(filename)
                .XPathSelectElement("//Tag[Name='PBAS' and ID='1']")
                .Element("Value")
                .Value;

or
var yourval = (string)XDocument.Load(filename)
                      .XPathSelectElement("//Tag[Name='PBAS' and ID='1']/Value");

or (using only Linq)
var yourval = XDocument.Load(filename)
              .Descendants("Tag")
              .Where(t => (string)t.Element("Name") == "PBAS")
              .Where(t => (string)t.Element("ID") == "1")
              .First()
              .Element("Value")
              .Value;

PS: Necessary namespaces: System.Xml, System.Xml.XPath, and System.Xml.Linq . Also Read about Linq2Xml and Xpath

