【发布时间】:2012-11-12 10:34:17
【问题描述】:
我创建了一个方法来检查 XML 文件中是否存在属性。如果它不存在,则返回“False”。它可以工作,但解析文件需要很长时间。它似乎读取每一行的整个文件。我在这里错过了什么吗?我可以让它更有效吗?
public static IEnumerable<RowData> getXML(string XMLpath)
{
XDocument xmlDoc = XDocument.Load("spec.xml");
var specs = from spec in xmlDoc.Descendants("spec")
select new RowData
{
number= (string)spec.Attribute("nbr"),
name= (string)spec.Attribute("name").Value,
code = (string)spec.Attribute("code").Value,
descr = (string)spec.Attribute("descr").Value,
countObject = checkXMLcount(spec),
return specs;
}
public static string checkXMLcount(XElement x)
{
Console.WriteLine(x.Attribute("nbr").Value);
Console.ReadLine();
try
{
if (x.Attribute("mep_count").Value == null)
{
return "False";
}
else
{
return x.Attribute("mep_count").Value;
}
}
catch
{
return "False";
}
}
我测试了用只返回和接收字符串的方法替换该方法:
public static string checkXMLcount(string x)
{
Console.WriteLine(x);
Console.ReadLine();
return x;
}
我制作了一个只有一行的 XML 文件。控制台打印出该值 15 次。有什么想法吗?
【问题讨论】:
-
我想知道为什么要编写自己的 XPath 版本?
标签: c# xml linq linq-to-xml