如何统计.resx文件中含<comment>节点的<data>节点数量?(Python/.NET可选)
统计.resx文件中包含
下面分别用Python和.NET控制台程序实现需求,针对你提供的示例片段,最终统计结果应为2。
Python实现
使用Python标准库的xml.etree.ElementTree解析XML,无需额外安装依赖:
import xml.etree.ElementTree as ET def count_data_with_comment(resx_path): # 解析resx文件 tree = ET.parse(resx_path) root = tree.getroot() count = 0 # 遍历所有<data>节点 for data_node in root.findall('data'): # 检查当前<data>节点是否包含<comment>子节点 if data_node.find('comment') is not None: count += 1 return count if __name__ == "__main__": # 替换为你的resx文件路径 resx_file = "resources.resx" result = count_data_with_comment(resx_file) print(f"包含comment子节点的data节点数量: {result}")
运行脚本后,控制台会直接输出符合条件的节点数量。
.NET(C#)控制台程序实现
利用.NET的System.Xml.Linq组件快速处理XML:
using System; using System.Xml.Linq; namespace ResxCommentCounter { class Program { static void Main(string[] args) { // 替换为你的resx文件路径 string resxPath = @"C:\path\to\your\resources.resx"; // 加载resx文件为XML文档 XDocument resxDoc = XDocument.Load(resxPath); // 统计包含<comment>子节点的<data>节点数 int matchingCount = resxDoc.Descendants("data") .Count(dataNode => dataNode.Element("comment") != null); Console.WriteLine($"包含comment子节点的data节点数量: {matchingCount}"); } } }
编译并运行该控制台程序,即可得到统计结果。
内容的提问来源于stack exchange,提问作者SaberTooth
相关产品推荐
相关产品推荐

