C# SelectNodes始终返回0计数
SelectNodes始终返回0计数,即使它具有该值C# SelectNodes始终返回0计数,c#,xml,C#,Xml,SelectNodes始终返回0计数,即使它具有该值 <?xml version="1.0" encoding="utf-16"?> <Configurations xmlns="DEH_Common.Schemas"> <sftpConfiguration> <file> <filedetails> <fileext>csv</f
<?xml version="1.0" encoding="utf-16"?>
<Configurations xmlns="DEH_Common.Schemas">
<sftpConfiguration>
<file>
<filedetails>
<fileext>csv</fileext>
<DataContentDetailId>1</DataContentDetailId>
</filedetails>
</file>
</sftpConfiguration>
</Configurations>
在XML中,
DocumentElement
是配置节点,因此XPath应该是sftpConfiguration/file
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("sftpConfiguration/file");
在XML中,
DocumentElement
是配置节点,因此XPath应该是sftpConfiguration/file
XmlNodeList nodeList = xmlDoc.DocumentElement.SelectNodes("sftpConfiguration/file");
试试这个:
xmlDoc.DocumentElement.SelectNodes("/Configurations[@*]/sftpConfiguration/file");
试试这个:
xmlDoc.DocumentElement.SelectNodes("/Configurations[@*]/sftpConfiguration/file");
这是因为在
xml
中使用了名称空间,所以您应该将名称空间添加到xmlDoc中,而且不需要使用DocumentElement`这段代码可以工作:
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("a", "DEH_Common.Schemas");
XmlNodeList nodeList = xmlDoc.SelectNodes("//a:sftpConfiguration/a:file", nsmgr);
这是因为在xml
中使用了名称空间,所以您应该将名称空间添加到xmlDoc中,而且不需要使用DocumentElement`这段代码可以工作:
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("a", "DEH_Common.Schemas");
XmlNodeList nodeList = xmlDoc.SelectNodes("//a:sftpConfiguration/a:file", nsmgr);
我得到了它。它与名称空间有关,我知道了。它与名称空间相关,编写了一个快速示例,并nodeList.Count
返回1
。必须删除xmlns属性并将编码更改为utf-8,然后编写一个快速示例并nodeList.Count
返回1
。必须删除xmlns属性,并将编码更改为utf-8,但仍然有效。谢谢,成功了。非常感谢。