Tôi đang cố gắng xóa nhiều nút mà một số element(path)
cụ thể chứa một giá trị nhưng tôi nhận được System.NullReferenceException
bất kỳ trợ giúp nào mà tôi đang gặp trục trặc mà tôi sẽ đánh giá cao.C# linq xóa nhiều nút
xml của tôi trông như thế này:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ApplicationData Version="12.5.1" RootPath="FireFox-FILES">
<RegistrySystem>
<DIR Operation="+" Path="C:\Temp\Microsoft\MediaPlayer\ShimInclusionList" />
<DIR Operation="+" Path="C:\Temp\MediaPlayer\ShimInclusionList\MM.EXE" />
<DIR Operation="+" Path="C:\Temp\MediaPlayer\ShimInclusionList\plugin-container.exe" />
<DIR Operation="+" Path="C:\Temp\Microsoft\MediaPlayer">
<ENTRY Name="" Value="43.0.4" Type="1" />
<ENTRY Name="CurrentVersion" Value="43.0.4 (x86 en-GB)" Type="1" />
</DIR>
<DIR Operation="+" Path="C:\Program Files\Microsoft\MediaPlayer\ShimInclusionList\plugin-container.exe" />
<DIR Operation="+" Path="C:\Program Files\Microsoft\MediaPlayer\ShimInclusionList2\plugin.exe" />
<DIR Operation="+" Path="C:\Program Files\Microsoft\MediaPlayer\ShimInclusionList2\container.exe" />
<DIR Operation="+" Path="C:\Program Files\Microsoft\MediaPlayer\ShimInclusionList4">
<ENTRY Name="" Value="43.0.4" Type="1" />
<ENTRY Name="CurrentVersion" Value="43.0.4 (x86 en-GB)" Type="1" />
</DIR>
</RegistrySystem>
</ApplicationData>
Mã của tôi trông như thế này:
XDocument xdoc = XDocument.Load(XmlFile);
foreach (var node in xdoc.Descendants("DIR").Where(status => status.Attribute("Path").Value.Contains(@"C:\Temp\")))
{
node.Remove();
}
xdoc.Save(XmlFile);
Tôi không chắc chắn nơi tôi sẽ sai.
[gì là một 'NullReferenceException' và làm thế nào để sửa chữa nó?] (Http: // stackoverflow .com/q/4660142/447156) –
Có thể 'xdoc' là null sau khi thử tải nó từ tệp XML không? – Codor
Xin lỗi, tôi có nghĩa là Thuộc tính. – BillyDay