XML : Access XML Elements using XElement

Given the XML:

  <Foo>    <DateOfIssue>      <Year>2016</Year>      <Month>1</Month>      <Day>16</Day>    </DateOfIssue>    <OtherTag>       ...    </OtherTag>  </Foo>    

There must be a better way to do this. So far I can only find this:

  Dim doc As XDocument = XDocument.Load("sample.xml")  Dim xml As XElement = doc.Root  Dim stuff As IEnumerable(Of XElement) = xml.Elements  For Each s In stuff      If s.Name.LocalName = "DateOfIssue" Then          PublishDate = cdate(s.Elements().FirstOrDefault(Function(x) x.Name.LocalName = "Month").Value & "/" & s.Elements().FirstOrDefault(Function(x) x.Name.LocalName = "Day").Value & "/" & s.Elements().FirstOrDefault(Function(x) x.Name.LocalName = "Year").Value)          Exit For      End If  Next    

I want to read the elements and build a date out of it. I've tried Descendants, Elements, and the funky s...<Year>.Value mentioned here https://msdn.microsoft.com/en-us/library/bb384974.aspx?f=255&MSPPError=-2147217396

Every other method I've tried returns Nothing. I must be missing something really simple, but I can't find it.

Thanks in advance.

No comments:

Post a Comment