Good Day,
I am trying to query an XML document and have the following query:
XElement root = XElement.Load(@"..\..\Data.xml");
var entries = root.Descendants()
.Where(x => x.Name.LocalName == "Entry")
.ToList();
Console.WriteLine("There are {0} nodes...", entries.Count());
foreach (XElement v in entries)
{
Console.WriteLine(v.Value);
}
and this code works because it pulls the correct number of Entry nodes. The Entry nodes look like:
<?xml version="1.0" encoding="UTF-8"?>
<Database xmlns="http://www.someurl.org/schemas">
<InfoFromRecord>
<BaseData>
<Entry>
<Date>2006-03-08</Date>XD
<Time>09:20:00</Time>
<EnteredBy>DNS</EnteredBy>
<TextEntry>Record 1</TextEntry>
</Entry>
<Entry>
<Date>2006-03-08</Date>
<Time>09:33:00</Time>
<EnteredBy>MW</EnteredBy>
<TextEntry>Record 2</TextEntry>
</Entry>
<Entry>
<Date>2006-03-08</Date>
<Time>08:58:00</Time>
<EnteredBy>BH</EnteredBy>
<TextEntry>Record 3</TextEntry>
</Entry>
</BaseData>
</InfoFromRecord>
</Database>
The problem is, I want to extract only the Date and Time, not all four fields.
Do not forget that
Descendants
finds children at any level, i.e. children, grand-children, etc whereElements
find only direct child. So i guessElements
is the safe option in most of the cases.EDIT : After seeing the XML
You need to include the Namspace also when getting the data
Let's assume your entire XML file looks like this for a clear example:
You could then do something like this:
From there, the
dateAndTimes
type will select an anonymous type of the Date and Time. You can change the anonymous type to be your own type, or something else.EDIT: The problem is your
xmlns
. Change your code like so:I haven't had a chance to try it but something like the following may give you what you are looking for