对于下面的xml文档,我显示当前所选文件夹的所有子文件夹和文件。对于xml字符串,我删除了不匹配的文件夹,并在前端使用XSLT构建了一个UI。似乎该操作正在删除使得结果字符串无效的所有节点xml。所以
<?xml version="1.0" encoding="utf-8"?>
<Folder>
<Folders>
<Folder ID="1" Name="Root" ParentId="0">
<Files></Files>
</Folder>
<Folder ID="2" Name="My Documents" ParentId="1">
<Files>
<File Name="LicenceCode.txt" Size="2000" CreatedOn="1/1/2012 12:12:00 PM" CreatedBy="1" ModifiedOn="1/10/2012 10:12:56 AM" ModifiedBy="2"></File>
</Files>
</Folder>
</Folders>
</Folder>
XElement filesAndFolders = XElement.Parse(xmlDocumentString);
string outputFolders = string.concat(from folders in filesAndFolders in filesAndFolder.Elements("Folder").Folder("Folders") where folders.Attribute("ParentId").Value.Equals(selectedFolderId) select folders);
//pass outputFolders string to xsl to build the UI
outputFolders
字符串无效,因为它仅包含字符串而非有效文档
<Folder ID="2" Name="My Documents" ParentId="1">
<Files>
<File Name="LicenceCode.txt" Size="2000" CreatedOn="1/1/2012 12:12:00 PM" CreatedBy="1" ModifiedOn="1/10/2012 10:12:56 AM" ModifiedBy="2"></File>
</Files>
</Folder>
答案 0 :(得分:1)
如何删除不需要的元素:
var doc = XDocument.Parse(xmlString);
doc.Element("Folder").Element("Folders").Elements("Folder").Where(f => f.Attribute("ID").Value == "1").Remove();