single expression to read value of element in XmlTextReader

100 views Asked by At

consider the part of xml file.

<Element>Value</Element>

i used XmlTextReader to read xml file. so i used while(reader.Read())

now when i reach Element i want to get its Value.

if(reader.NodeType == XmlNodeType.Element && reader.LocalName == "Element")
{
     reader.Read();
     var var = reader.Value;
}

is there any way to combine this two lines? a single expression equals to:

     reader.Read();
     var var = reader.Value;

Note that i dont want to skip into Element. i am using XmlTextReader and i read xml line by line.

the only reason is to keep the code simple. i feel better if this be possible.

1

There are 1 answers

0
Daniel Gimenez On BEST ANSWER

You could just create an extension method

public static string ReadValue(this XmlTextReader reader) 
{
  reader.Read();
  return reader.Value;
}

Usage:

if(reader.NodeType == XmlNodeType.Element && reader.LocalName == "Element")
{
     var var = reader.ReaderValue();
}