使用Linq to XML选择Xml节点

6

我的 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>  

my attempt :

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

这会抛出以下异常:

 Object reference not set to an instance of an object.

1) x 不是一个 XElement 吗?

2) 这个 where lambda 表达式用于选择 Xml 节点是否正确?

3) 当然,你会如何根据 CustomerId 找到这个节点?


我最初遇到了这个异常: 无法将类型为“WhereEnumerableIterator`1[System.Xml.Linq.XElement]”的对象转换为类型“System.Xml.Linq.XElement”。 这是因为我试图从IEnumerable转换为单个XElement,所以我添加了一个First<XElement>()扩展方法。 现在出现问题的是它无法识别x作为一个XElement。 - eran otzap
4个回答

4

你的问题在于DescendentsWhere返回的是一个IEnumerable<XElement>而不是你需要的单个XElement。你可以通过以下方式解决:

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

请注意,这需要在Customer下只有一个CustomerId元素。如果为0或> 1,则会抛出异常。看了他的XML后,这可能是适当的。但只是指出一些事情。 - Andrew T Finnell
@AndrewFinnell 在这个话题上,你会如何编辑那个节点?我现在可以更新所有客户的(XElement)后代,但是如何更新文件中的节点呢? - eran otzap

2

您要转换的不是x,而是x.Descendants()。x.Descendants()返回一个集合,因此使用了复数方法语义。我能想到的是,您应该可以这样做:x.Descendants("CustomerId").FirstOrDefault() as XElement


1
XElement toEdit = (from c in doc.Descendants("Customer")
     where Guid.Parse(c.Value) == customer.CustomerId
     select c).SingleOrDefault();

1

我会像这样重新构建你的查询:

 XElement toEdit = doc.Descendants("Customer")
                      .Where(x => (Guid)x.Element("CustomerId") == customer.CustomerId)
                      .FirstOrDefault();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接