【问题标题】:How to filter Xdocument and return Xdocument?如何过滤 Xdocument 并返回 Xdocument?
【发布时间】:2013-11-14 18:34:04
【问题描述】:

我的应用程序从 SharePoint Web 服务获取数据(使用 SOAP 和 CAML 查询),我使用 Xdocument 文档来存储检索到的 xmlNode,然后将 xdocument 分配给 XMLDataSource,该 XMLDataSource 被绑定到 gridView。

现在我需要在绑定之前过滤 Xdocument,只选择元素 (ows_Partner_x0020_Type) 与变量匹配的那些记录。

我正在尝试这样:

doc = doc.Descendants(z + "row").Where(rows => rows.Attribute("ows_Partner_x0020_Type").Value == Partner_Type.SelectedValue);

var bar = doc.Descendants(z + "row").Where(rows => rows.Attribute("ows_Partner_x0020_Type").Value == Partner_Type.SelectedValue);

但问题是上述LINQ的返回类型是System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>

它与 XDocument 完全不同,它是作为 doc.ToString() 绑定到 XMLDataSource 所需的格式。

希望我能够解释这个问题。

非常感谢。

维沙尔

【问题讨论】:

    标签: c# asp.net linq lambda linq-to-xml


    【解决方案1】:

    如果你只是想用这些元素创建一个文档,你可以使用:

    XDocument filteredDocument = new XDocument(new XElement("root", bar));
    

    (这将创建一个根元素为 <root> 的文档,以及您感兴趣的所有元素。)

    不太确定所有绑定部分 - 我强烈怀疑可能有更好的选择 - 但这肯定会给你一个新的XDocument

    【讨论】:

    • 谢谢,这似乎有效,问题是它创建了三次相同的结构。比如 base {System.Xml.Linq.XContainer}: 声明:null DocumentType:null NodeType:Document Root:
    • 顺便说一句,bar的类型是:System.Linq.Enumerable.WhereEnumerableIterator
    • @VishalSachdeva:很难说出你真正拥有什么。如果您将整个文档作为 XML 文档 转储出来,会发生什么?我怀疑你只有 3 个匹配的行...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    相关资源
    最近更新 更多