string GetPreciseXPath(XElement element) {
if (element.Parent == null)
return element.Name.LocalName;
int index = element.Parent.Elements(element.Name).ToList().IndexOf(element) + 1;
return GetPreciseXPath(element.Parent) + $"/{element.Name.LocalName}[{index}]";
}
string GetXPathWithAttribute(XElement element, string attrName) {
string attrValue = (string)element.Attribute(attrName);
return $"//{element.Name.LocalName}[@{attrName}='{attrValue}']";
}
// 使用示例:生成路径如 "//Employee[@id='001']"
发表评论 取消回复