6

my Xml file :

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Customer>
        <CustomerId>1f323c97-2015-4a3d-9956-a93115c272ea</CustomerId>
        <FirstName>Aria</FirstName>
        <LastName>Stark</LastName>
        <DOB>1999-01-01T00:00:00</DOB>
    </Customer>
    <Customer>
        <CustomerId>c9c326c2-1e27-440b-9b25-c79b1d9c80ed</CustomerId>
        <FirstName>John</FirstName>
        <LastName>Snow</LastName>
        <DOB>1983-01-01T00:00:00</DOB>
    </Customer>
</ArrayOfCustomer>  

my attempt :

XElement toEdit = 
    (XElement)doc.Descendants("ArrayOfCustomer")
                 .Descendants("Customer")
                 .Where(x => Guid.Parse((x.Descendants("CustomerId") as XElement).Value) == customer.CustomerId)
                 .First<XElement>();

this throws the following exception :

 Object reference not set to an instance of an object.

1) isn't x an XElement?

2) is this a proper where lambda for selecting an Xml node?

3) and of course how would you find this node according to CustomerId?

Darko
  • 38,310
  • 15
  • 80
  • 107
eran otzap
  • 12,293
  • 20
  • 84
  • 139
  • i first got the exception : Unable to cast object of type 'WhereEnumerableIterator`1[System.Xml.Linq.XElement]' to type 'System.Xml.Linq.XElement'. this was because i tried to cast from IEnumrable to a single XElement , i added a First() extension to it . now it just can't make out x as an XElement . – eran otzap Apr 10 '12 at 03:55

4 Answers4

4

Your problem is that Descendents and Where return an IEnumerable<XElement> not a single XElement which is what you're after. You can fix this like this:

XElement toEdit = doc.Descendants("ArrayOfCustomer")
                     .Descendants("Customer")
                     .Where(x => Guid.Parse(x.Descendants("CustomerId").Single().Value) == customer.CustomerId)
                     .FirstOrDefault();
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • Just be aware this requires there be only one CustomerId element beneath a Customer. If there is 0 or > 1 it will throw an exception. And after looking at his XML it is probably appropriate. But just something to point out. – Andrew T Finnell Apr 10 '12 at 03:59
  • @AndrewFinnell while were on the subject , how would you go about editing that node , i can now update all of Customer's (XElement) decedents but how would update the node in the file? – eran otzap Apr 10 '12 at 04:06
2

You are not casting x you are casting x.Descendants(). x.Descendants() returns a collection, hence the plural method semantic. Off the top of my head you should be able to do x.Descendants("CustomerId").FirstOrDefault() as XElement

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
1
XElement toEdit = (from c in doc.Descendants("Customer")
     where Guid.Parse(c.Value) == customer.CustomerId
     select c).SingleOrDefault();
ionden
  • 12,536
  • 1
  • 45
  • 37
1

I would restructure your query like this:

 XElement toEdit = doc.Descendants("Customer")
                      .Where(x => (Guid)x.Element("CustomerId") == customer.CustomerId)
                      .FirstOrDefault();
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335