我正在通过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?
检查有关XmlDocument.GetElementById方法的MSDN文档:
DOM实现必须具有定义哪些属性为ID类型的信息。尽管可以在XSD架构或DTD中定义类型ID的属性,但是此产品版本仅支持在DTD中定义的属性。除非在DTD中定义,否则名称为“ ID”的属性的类型不是ID。未知属性是否为ID类型的实现应返回null。
实际上,您必须修改XML文件以指定“ ID”的含义。如果您不想这样做,请对XPath使用select方法。
因此,您需要:
string filePath = "D:\\XML\\LanguagePack.xml";
var fileInfo = new FileInfo(filePath);
var xmlDocument = new XmlDocument();
xmlDocument.Load(fileInfo.FullName);
var node = xmlDocument.SelectSingleNode("//*[@id='10001']");
return node.InnerText; // return 'Word1_French'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句