tags:

views:

150

answers:

3

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, (the editor has not removed or duplicated/multiplid the item).

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).

A: 

try this

 if (DB_English.ToString() != Records.Elements("English").InnerXML.ToString())

Kindness,

Dan

Daniel Elliott
Morning Dan,Tried that, compiler threw this...Error 1 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'InnerXML' and no extension method 'InnerXML' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?)
Fossaw
A: 
Records.Elements("English").Value

This will give you the value of the XElement. But it will only return text. If there is html/xml in the value then that will not work and you will need to write an extension method to get this value.

In that case, this link should help: http://blogs.msdn.com/ericwhite/archive/2008/12/22/convert-xelement-to-xmlnode-and-convert-xmlnode-to-xelement.aspx

Joseph Connolly
Same kind of error as above... .Value, or .Value().Error 2 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' could be found (are you missing a using directive or an assembly reference?)
Fossaw
A: 

I added this...

XElement X_EnglishE = null;  // This is BLOODY CRAZY
foreach (XElement i in Records)
    {
        X_EnglishE = i.Element("English");  // There is only one damned record!
    }
string X_English = X_EnglishE.Value.ToString();

... and test the resulting X_English string. This works but seems ridiculous! There HAS to be a better way.

Fossaw