LINQ to Xml 帮助
我之前发布了另一篇关于如何使用 XSLT 转换 XmlDocument 的文章,但也许可以使用 LINQ 来代替。
我有一个类似于以下内容的 XmlDocument:
<DocumentElement>
<Customer>
<CustomerId>2315</CustomerId>
<VersionNumber>1</VersionNumber>
<GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
<Customer>
<CustomerId>2316</CustomerId>
<VersionNumber>2</VersionNumber>
<GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
<Customer>
<CustomerId>2317</CustomerId>
<VersionNumber>1</VersionNumber>
<GUID>9995DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
</DocumentElement>
我可以使用 LINQ 为每个唯一的 GUID 提取一个客户元素并获取具有最高版本号的客户吗?
即新的/转换后的文档将如下所示:
<DocumentElement>
<Customer>
<CustomerId>2316</CustomerId>
<VersionNumber>2</VersionNumber>
<GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
<Customer>
<CustomerId>2317</CustomerId>
<VersionNumber>1</VersionNumber>
<GUID>9995DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
</DocumentElement>
提前致谢。
更新:
所以我应该这样做(如果我理解正确的话?):
XDocument xdoc = new XDocument();
using (StringWriter sw = new StringWriter())
{
// Converts the datatable to XML
dt.WriteXml(sw);
xdoc = XDocument.Parse(sw.ToString());
}
var query = xdoc.Root
.Elements("Customer")
.GroupBy(x => x.Element("GUID").Value)
.Select(g => g.OrderByNumberDescending(x =>(int)x.Element("VersionNumber"))
.First());
我仍然在 GroupBy 上收到错误,或者我在这里做错了什么?
I posted another post earlier on on how to transform an XmlDocument with XSLT but maybe it is possible to do with LINQ instead.
I have an XmlDocument that looks something like:
<DocumentElement>
<Customer>
<CustomerId>2315</CustomerId>
<VersionNumber>1</VersionNumber>
<GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
<Customer>
<CustomerId>2316</CustomerId>
<VersionNumber>2</VersionNumber>
<GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
<Customer>
<CustomerId>2317</CustomerId>
<VersionNumber>1</VersionNumber>
<GUID>9995DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
</DocumentElement>
Can I, with LINQ, extract one customer element for each unique GUID and get the customer with the highest version number?
I.e. the new/transformed document will look like:
<DocumentElement>
<Customer>
<CustomerId>2316</CustomerId>
<VersionNumber>2</VersionNumber>
<GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
<Customer>
<CustomerId>2317</CustomerId>
<VersionNumber>1</VersionNumber>
<GUID>9995DE20-02A0-425D-944D-65E5E744FF8A</GUID>
</Customer>
</DocumentElement>
Thanks in advance.
Update:
So I should do (if I understand you correctly?):
XDocument xdoc = new XDocument();
using (StringWriter sw = new StringWriter())
{
// Converts the datatable to XML
dt.WriteXml(sw);
xdoc = XDocument.Parse(sw.ToString());
}
var query = xdoc.Root
.Elements("Customer")
.GroupBy(x => x.Element("GUID").Value)
.Select(g => g.OrderByNumberDescending(x =>(int)x.Element("VersionNumber"))
.First());
I still get the error on GroupBy though, or am I doing something else wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对可以:
或者使用 MoreLINQ:
Absolutely:
Or using MoreLINQ: