c#读取xml文件,节点值返回为空

本文关键字:返回 节点 读取 xml 文件 | 更新日期: 2023-09-27 18:01:41

基本上,处理一个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文件,节点值返回为空

Try InnerText

countryname = countryNode.InnerText;