You can use following class to deserialize the xml into C# class object and then easily can access any property to get desired value
POC Code:
[XmlRoot(ElementName="CollectionDetails")]
public class CollectionDetails {
[XmlElement(ElementName="Collection")]
public string Collection { get; set; }
[XmlElement(ElementName="Year")]
public string Year { get; set; }
[XmlElement(ElementName="FilePreparationDate")]
public string FilePreparationDate { get; set; }
}
[XmlRoot(ElementName="Source")]
public class Source {
[XmlElement(ElementName="ProtectiveMarking")]
public string ProtectiveMarking { get; set; }
[XmlElement(ElementName="UKPRN")]
public string UKPRN { get; set; }
[XmlElement(ElementName="TransmissionType")]
public string TransmissionType { get; set; }
[XmlElement(ElementName="SoftwareSupplier")]
public string SoftwareSupplier { get; set; }
[XmlElement(ElementName="SoftwarePackage")]
public string SoftwarePackage { get; set; }
[XmlElement(ElementName="Release")]
public string Release { get; set; }
[XmlElement(ElementName="SerialNo")]
public string SerialNo { get; set; }
[XmlElement(ElementName="DateTime")]
public string DateTime { get; set; }
}
[XmlRoot(ElementName="Header")]
public class Header {
[XmlElement(ElementName="CollectionDetails")]
public CollectionDetails CollectionDetails { get; set; }
[XmlElement(ElementName="Source")]
public Source Source { get; set; }
}
Sample code:
XmlSerializer serializer = new XmlSerializer(typeof(Header));
StreamReader reader = new StreamReader(path);
var header = (Header)serializer.Deserialize(reader);
reader.Close();
//use the header object to access your data
Update
Here's another way using xpath:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.XPath;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
XPathNavigator nav;
XPathDocument docNav;
string xPath;
docNav = new XPathDocument(AppDomain.CurrentDomain.BaseDirectory + "test.xml");
nav = docNav.CreateNavigator();
xPath = "/Header/CollectionDetails/Year/text()";
string value = nav.SelectSingleNode(xPath).Value;
Console.WriteLine(value);
}
}
}