我的应用程序从 SharePoint Web 服务获取数据(使用 SOAP 和 CAML 查询),我使用 Xdocument 文档来存储检索到的 xmlNode,然后将 xdocument 分配给绑定(bind)到 gridView 的 XMLDataSource。
现在我需要在绑定(bind)之前过滤 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 完全不同,XDocument 是作为 doc.ToString() 绑定(bind)到 XMLDataSource 所需的格式。
希望我能够解释这个问题。
提前非常感谢。
维沙尔
最佳答案
如果您只是尝试使用这些元素创建文档,您可以使用:
XDocument filteredDocument = new XDocument(new XElement("root", bar));
(这将创建一个根元素为 <root>
的文档,以及您感兴趣的所有元素直接位于其下。)
不太确定所有的绑定(bind)部分 - 我强烈怀疑可能有更好的选择 - 但这肯定会给你一个新的 XDocument
.
关于c# - 如何过滤Xdocument并返回Xdocument?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19985555/