本文介绍了将 SelectSingleNode 与 XPath 一起使用返回 NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用 SelectSingleNode
修改 XML 文件.文件结构为
I trying to modify an XML file with SelectSingleNode
.The structure of file is
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ProjectExtensions>
<Borland.Personality>Delphi.Personality</Borland.Personality>
<Borland.ProjectType>VCLApplication</Borland.ProjectType>
<BorlandProject>
<BorlandProject>
<Delphi.Personality>
<Parameters>
...
</Parameters>
<VersionInfo>
<VersionInfo Name="IncludeVerInfo">True</VersionInfo>
<VersionInfo Name="AutoIncBuild">False</VersionInfo>
<VersionInfo Name="MajorVer">4</VersionInfo>
<VersionInfo Name="MinorVer">1</VersionInfo>
<VersionInfo Name="Release">3</VersionInfo>
<VersionInfo Name="Build">559</VersionInfo>
<VersionInfo Name="Debug">False</VersionInfo>
<VersionInfo Name="PreRelease">False</VersionInfo>
<VersionInfo Name="Special">False</VersionInfo>
<VersionInfo Name="Private">False</VersionInfo>
<VersionInfo Name="DLL">False</VersionInfo>
<VersionInfo Name="Locale">1049</VersionInfo>
<VersionInfo Name="CodePage">1251</VersionInfo>
</VersionInfo>
...
...
...
我在 VS C# 上的代码是
My code on VS C# is
using System.Xml;
namespace xmledit
{
class Program
{
private static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("arm.xml");
var node = doc.SelectSingleNode("//VersionInfo[@Name='Build']");
if (node != null)
node.InnerText = "123";
doc.Save("temp.xml");
}
}
}
所以,我试图用 Name="Build"
修改标签 VersionInfo
,但 SelectSingleNode
返回 NULL
.我做错了什么?
So, i trying to modify Tag VersionInfo
with Name="Build"
, but SelectSingleNode
returns NULL
.What I doing wrong?
推荐答案
你的 xml 文档有一个默认的命名空间 xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
因此(我假设)您需要使用 XmlNamespaceManager.
Your xml document has an default namespace xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
therefore (I assume) you need to use a XmlNamespaceManager.
- http://msdn.microsoft.com/en-us/library/h0hw012b.aspx
- http://msdn.microsoft.com/en-us/library/d6730bwt.aspx
这篇关于将 SelectSingleNode 与 XPath 一起使用返回 NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!