1

I read through this post.

I have this XML:

<?xml version="1.0" encoding="utf-8" ?>
    <Export version="" srcSys="" dstSys="" srcDatabase="" timeStamp="">

    </Export>

This is what i tried, but with no luck:

var xml = XElement.Parse(BuyingModule.Properties.Resources.Export);

Func<XElement, string, string> GetAttribute = (e, property) => e.Elements("property").Where(p => p.Attribute("name").Value == property).Single().Value;

var query = from record in xml.Elements("Export")
            select record;

var prop = GetAttribute(query.FirstOrDefault(), "version");

How do i access to properties of the "Export" Node?

I need to set those properties

Community
  • 1
  • 1
Willem
  • 9,166
  • 17
  • 68
  • 92

1 Answers1

3

The Export element doesn't have a properties element, which is what your GetAttribute method is trying to find.

My guess is you actually want:

var element = xml.Element("Export"); // Just get the first element
var version = (string) element.Attribute("version");

It's not clear to me why you've used a query expression and a delegate here - it's just things more complicated than you need. But Attribute(XName) is probably what you were missing...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Thanks for the help Jon. Looked like i didn't need the first line tho(`var element = xml.Element("Export");)`. I went straight: `var version = xml.Attribute("version");` – Willem Sep 22 '11 at 08:54
  • @Willem: Ah - I assumed that because you were looking for `Export` elements in your query, that the XML you'd actually parsed had a top level element above it... – Jon Skeet Sep 22 '11 at 08:59