C#读取XML不会仅为一个节点返回内部文本 [英] C# reading XML does not return innertext only for one node
问题描述
我有以下XML
<?xml version="1.0" encoding="utf-8" ?>
<Configuration>
<AppSettings>
<IPath>C:\Data\Files</IPath>
<ICode>DocCode</ICode>
<zipFile>C:\Data\Files\zip</zipFile>
<ConnectionString>Data Source=oracledb</ConnectionString>
</AppSettings>
</Configuration>
我的代码是获取Inputfilepath,Icode,zipfile,connectionstring等的值.我有以下代码来读取内部文本
My code is to get the values of Inputfilepath, Icode,zipfile,connectionstring etc. I have the following code to read the innertext
xmDoc = new XmlDocument();
xmDoc.Load(xmlFilePath);
XmlNode oXmlNode;
oXmlNode = xmDoc.SelectSingleNode("Configuration/AppSettings/ICode");
if (oXmlNode != null)
{
Icode= oXmlNode.InnerText;
}
oXmlNode= xmDoc.SelectSingleNode("Configuration/AppSettings/zipfile");
if (oXmlNode != null)
{
zfile = oXmlNode.InnerText;
}
oXmlNode= xmDoc.SelectSingleNode("Configuration/AppSettings/IPath");
if (oXmlNode != null)
{
iPath = oXmlNode.InnerText;
}
问题仅在于Icode,而Zipfile标记正在获取内部文本值并将其分配给变量.但是IPath始终返回null.我知道我忽略了一些非常琐碎的事情,但无法解决.
The issue is only the Icode, and Zipfile tags are getting innertext value and being assigned to the variables. but the IPath is always returning null. I know am overlooking something very trivial but can't figure it out.
谢谢
推荐答案
我测试了您的代码,除了一些拼写错误(例如"zipfile"而不是"zipFile")外,我没有看到任何问题.这是我的Console.WriteLine结果:
I tested your code and besides some misspellings (e.g. 'zipfile' instead of 'zipFile'), I didn't see any problems. Here my Console.WriteLine results:
C:\ Data \ Files
DocCode
C:\ Data \ Files \ zip
C:\Data\Files
DocCode
C:\Data\Files\zip
因此,请注意区分大小写并检查代码中是否存在拼写错误.
So, mind the case sensitivity and check your code for any misspellings.
wizend
这篇关于C#读取XML不会仅为一个节点返回内部文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!