tập tin Xml của tôi:Chọn Xml Node sử dụng LINQ to XML
<?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>
nỗ lực của tôi:
XElement toEdit =
(XElement)doc.Descendants("ArrayOfCustomer")
.Descendants("Customer")
.Where(x => Guid.Parse((x.Descendants("CustomerId") as XElement).Value) == customer.CustomerId)
.First<XElement>();
này ném ngoại lệ sau đây:
Object reference not set to an instance of an object.
1) không phải là x
an XElement
?
2) đây có phải là nơi thích hợp để chọn một nút Xml không?
3) và tất nhiên bạn sẽ tìm thấy nút này theo số CustomerId
như thế nào?
tôi lần đầu tiên có trường hợp ngoại lệ: Không thể cast đối tượng của loại 'WhereEnumerableIterator'1 [Hệ thống .Xml.Linq.XElement] 'để nhập' System.Xml.Linq.XElement '. điều này là do tôi đã cố gắng truyền từ IEnumrable sang một XElement, tôi đã thêm phần mở rộng Đầu tiên() vào nó. bây giờ nó chỉ không thể tạo ra x như một XElement. –