【发布时间】:2013-11-16 04:42:15
【问题描述】:
我正在通过 id 使用 XML 创建语言翻译
XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<word id="1">Word1_English</word>
<word id="2">Word2_English</word>
<word id="3">Word3_English</word>
<word id="10001">Word1_French</word>
<word id="10002">Word2_French</word>
<word id="10003">Word3_French</word>
<word id="20001">Word1_Chinese</word>
<word id="20002">Word2_Chinese</word>
<word id="20003">Word3_Chinese</word>
</root>
后面的代码:
XmlDocument xmlDocument;
FileInfo fileInfo;
XmlNodeList xmlNodeList;
string xPath = "D:\XML\LanguagePack.xml";
fileInfo = new FileInfo(xPath);
xmlDocument = new XmlDocument();
xmlDocument.Load(fileInfo.FullName);
xmlNodeList = xmlDocument.GetElementById("10001");
return xmlNodeList[0].InnerText; //should return 'Word1_French'
此代码不起作用,xmlNodeList 为空。
如何获取 Word1_French 内容?
【问题讨论】:
-
它现在返回什么?
-
贴出的代码有问题吗?
-
由于 GetElementById 只能返回单个元素,您应该将结果分配给 XmlNode,而不是 XmlNodeList。
-
谢谢大家的回答,不过西里尔的回答符合我的要求