Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

c# - How can I get a node by id in XML?

I am creating a language translation using XML by id

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>

Code behind:

XmlDocument xmlDocument;
FileInfo fileInfo;
XmlNodeList xmlNodeList;

string xPath = "D:XMLLanguagePack.xml";
fileInfo = new FileInfo(xPath);
xmlDocument = new XmlDocument();
xmlDocument.Load(fileInfo.FullName);

xmlNodeList = xmlDocument.GetElementById("10001");
return xmlNodeList[0].InnerText; //should return 'Word1_French'

This code doesn't work, xmlNodeList is null.
How can I get the content Word1_French?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check the MSDN documentation on XmlDocument.GetElementById Method:

The DOM implementation must have information which defines which attributes are of type ID. Although attributes of type ID can be defined in either XSD schemas or DTDs, this version of the product only supports those defined in DTDs. Attributes with the name "ID" are not of type ID unless so defined in the DTD. Implementations where it is unknown whether the attributes are of type ID are expected to return null.

In fact, you have to modify your XML file for specifying what you mean by 'ID'. If you don't want to do that, use a select method with an XPath.

So you need instead :

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...