【发布时间】:2021-08-14 21:03:09
【问题描述】:
我已经阅读了所有或几乎所有标签 linq-to-xml 以在我的例程中找到 where 子句的正确用法...但我无法找到解决方案... 有人可以帮帮我吗?
按照我丑陋的代码...
public string ExtractTag(string fileName, string tagName)
{
string fileBuffer = "";
using (StreamReader sr = new StreamReader(fileName))
{
fileBuffer = sr.ReadToEnd();
}
XDocument doc = XDocument.Parse(fileBuffer);
//Debug.WriteLine("doc.DescendantNodes().Count()=" + doc.DescendantNodes().Count());
foreach (XNode node in doc.DescendantNodes())
{
if (node is XElement)
{
XElement element = (XElement)node;
// Search the root node
if (element.Name.LocalName.Equals("FatturaElettronica"))
{
// Enumerate all nodes of "FatturaElettronica"
IEnumerable<XElement> de = from el in element.Descendants() select el;
foreach (XElement el in de)
{
if (string.Compare(el.Name.ToString(), tagName) == 0)
{
return el.Value;
}
}
}
}
}
return string.Empty;
}
【问题讨论】:
-
有什么问题?您当前的代码是否有效?有性能问题吗?您能否分享一个完整的minimal reproducible example,显示当前代码未按要求执行的 XML?为什么只检查本地名称而不检查命名空间?