我做了一个方法来检查属性是否存在于XML文件中。如果它不存在,则返回“False”。它可以工作,但解析文件需要很长时间。它似乎读取每一行的整个文件。我在这里错过了什么吗?我能否以某种方式使它更有效?XML解析检查属性是否存在
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版本,我想知道? – raina77ow