【问题标题】:C# reading an xml file, node value returns as blankC#读取xml文件,节点值返回为空白
【发布时间】:2015-07-08 13:20:08
【问题描述】:

所以基本上,处理一个 xml 文件,看起来像这样:

...
<city id="thatstheid">
  <country id="anotherid"> VALUE </country>
</city>
...

我阅读了我需要使用的东西:

XmlDocument doc;//let's say this is the file im reading

XmlNode cityNode = doc.DocumentElement.SelectSingleNode("city");
cityname = cityNode.Attributes["id"].Value;

XmlNode countryNode = cityNode.SelectSingleNode("country");
countryname = countryNode.Value;

这里的问题是

countryname = countryNode.Value;

返回一个空值,即使里面有东西。

如果我尝试像这样从内部获取任何属性:

countryname = countryNode.Attributes["id"].Value;

它工作正常,所以我不知道是什么问题。

【问题讨论】:

标签: c# xml


【解决方案1】:

试试 InnerText

countryname = countryNode.InnerText;

【讨论】:

  • 是的,似乎有效,非常感谢,知道为什么 .value 无效吗?编辑:没关系,看到我的主要帖子的评论。
猜你喜欢
  • 2010-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-21
  • 1970-01-01
  • 2010-11-14
相关资源
最近更新 更多