使用C#解析window_dump.xml并提取text与bounds数据至指定列表
提取结果
以下是从window_dump.xml中提取的所有符合格式的条目:
text=""|bounds="[0,0][1080,2076]" text=""|bounds="[0,0][1080,2076]" text=""|bounds="[0,108][1080,2076]" text=""|bounds="[0,108][1080,2076]" text=""|bounds="[0,108][1080,264]" text=""|bounds="[0,108][1080,264]" text=""|bounds="[0,108][1080,264]" text=""|bounds="[0,108][48,264]" text=""|bounds="[48,108][1032,264]" text="Confirm by email"|bounds="[48,108][452,264]" text=""|bounds="[1032,108][1080,264]" text=""|bounds="[0,262][1080,264]" text=""|bounds="[0,264][1080,2076]" text=""|bounds="[0,264][1080,2076]" text=""|bounds="[0,264][1080,2076]" text=""|bounds="[0,264][1080,2076]" text=""|bounds="[0,264][1080,2076]" text="We couldn't reach you at thuhoarfhhfvh68@gmail.com. Please enter another email to continue."|bounds="[36,336][1044,551]" text=""|bounds="[36,587][1044,724]" text="Email address"|bounds="[36,587][1044,724]" text="Update email address"|bounds="[36,760][1044,889]" text=""|bounds="[36,925][1044,1102]" text="Confirm by phone number"|bounds="[36,925][1044,1066]" text=""|bounds="[36,1102][1044,2058]" text=""|bounds="[36,1102][1044,1900]" text="Not you?"|bounds="[437,1900][643,1983]" text="Log Out"|bounds="[457,1983][622,2058]" text=""|bounds="[0,0][1080,108]" text=""|bounds="[0,0][0,0]"
C#实现代码
用XDocument解析XML,遍历所有node元素提取指定属性并按格式输出:
using System; using System.IO; using System.Xml.Linq; class Program { static void Main() { // 替换为你的XML文件实际路径 string xmlFilePath = "window_dump.xml"; XDocument xmlDoc = XDocument.Load(xmlFilePath); foreach (var nodeElement in xmlDoc.Descendants("node")) { string textValue = nodeElement.Attribute("text")?.Value ?? string.Empty; string boundsValue = nodeElement.Attribute("bounds")?.Value ?? string.Empty; Console.WriteLine($"text=\"{textValue}\"|bounds=\"{boundsValue}\""); } } }
说明
- 代码会加载指定路径的XML文件,递归遍历所有层级的
node节点 - 自动处理属性不存在的情况,用空字符串替代
- 结果直接输出到控制台,如需写入文件,可将
Console.WriteLine替换为文件写入逻辑
内容的提问来源于stack exchange,提问作者vinh nguyen
相关产品推荐
相关产品推荐

