c# - SelectSingleNode() 返回 null

标签 c# xml

感谢您阅读我的帖子。我试图从以下 xml 文件加载 FFTExe 的值,但它节点返回 null。

这是我用来读取 xml 的代码

    const string CMD_FFT = "/Command/FFTExe";    

    string strFile = Application.Current.Resources["ApplicationSettingsFolder"].ToString() + "\\CommandList.xml";
    XmlDocument doc = new XmlDocument();
    doc.Load(strFile);

    XmlNode node = doc.SelectSingleNode(CMD_FFT);
    if (null != node)  //<-----problem occurs here, node is null!!!
        GetAttribute(node, doc, "value", ref FFTExeFile);

这是我的 xml 文件,我想从条目 FFTExe 的值中提取“D:\fft\fft.exe”

<?xml version="1.0" ?>
<CommandList>
    <Command name="Capture" guid="30db4357-7508-46c9-84eb-3ca0900aa4c5" panel=".\Modules\ExperimentSettingsViewer.dll" description="">
        <Experiment value="" />
        <OutputPath value="" />
    </Command>
    <Command name="Run ImageJ Macro" guid="C1391459-132F-45ea-AE72-D7BEB2BD8086" panel=".\Modules\Panel.dll" description="">
        <Macro value="" />
        <DataFolder value="D:\out\" />
        <FFTExe value="D:\fft\fft.exe" />
        <Headless value="1" />
    </Command>
</CommandList>

感谢您的任何建议。

我也尝试过

const string CMD_FFT = "/Command/Command/FFTExe"; 

const string CMD_FFT = "FFTExe"; 

运气不好

最佳答案

将 XPath 更改为 /CommandList/Command/FFTExe,因为开头的单斜杠意味着从根开始,因此您需要包含 CommandList 根元素。

有关 XPath 的其他信息,请参阅此 link

关于c# - SelectSingleNode() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23295867/

相关文章:

python - 使用 Python 将 XML 文档插入现有 XML

c# - 用类常量值填充 List<string> 的方法。 C#

xml - 如何在 XPath 中识别具有相同名称的多个元素?

java - java中如何将大字符串保存到xml文件中?

c# - 不按字节执行按位

XML 架构 : Child element type based on parent attribute 'type'

java - 使用 XSLT 格式化 XML 转换的输出以生成文本文件

c# - 为什么 Console.Writeline、Console.Write 在 Visual Studio Express 中不起作用?

c# - JsonConvert 属性似乎被跳过。为什么?

c# - Entity Framework Core 首次加载缓慢