LINQ to XML contents of child records.
Posted
by Fossaw
on Stack Overflow
See other posts from Stack Overflow
or by Fossaw
Published on 2010-04-22T14:12:10Z
Indexed on
2010/04/23
14:03 UTC
Read the original article
Hit count: 335
I have this LINQ to XML enquiry...
var Records = from Item in XDoc.Root.Elements("Item")
where (string)Item.Element("ItemNumber") == item.ID.ToString
select Item;
... where ItemNumber is a reference number used in the XML, (originally written by this program but manually edited by "others"), and item.ID is the database version of the same thing. The query executes, and I can test for the number of entries in the result fine...
if (Records.Count() < 1)
... you get the idea. I have established that there is only one record.
Each Item has several child fields. I want to test the values of the child fields are reasonable before passing them on to the database update sub-system. The XML is produced by the program, but edited by users, so I need to really check what is coming back. So I tried...
if (DB_English.ToString() != Records.Elements("English").ToString())
... DB_English is from the database, but the XML in Records, does not contain the contents of that field, it contains...
System.Xml.Linq.Extensions+<GetElements>d__29`1[System.Xml.Linq.XElement]
... so, how do I get the value of this element in the XML file? I need to check the field in the XML has not been altered, (the manual editors of this data file are not potentially 100% reliable).
© Stack Overflow or respective owner