How can I get a node by id in XML?

2020-02-13 03:31发布

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:\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'

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

2条回答
我只想做你的唯一
2楼-- · 2020-02-13 03:56

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'
查看更多
男人必须洒脱
3楼-- · 2020-02-13 04:13

I suggest you to use LINQ to XML for parsing XML. It has nice strongly typed API. Getting string word by integer id looks like (requires System.Xml.Linq namespace):

var xdoc = XDocument.Load(filePath);
string word = xdoc.Root.Elements()
                  .Where(w => (int)w.Attribute("id") == id)
                  .Select(w => (string)w)
                  .FirstOrDefault();

Or even less code with XPath (requires System.Xml.XPath namespace):

string word = (string)xdoc.XPathSelectElement("//word[@id='10001']");
查看更多
登录 后发表回答