我做了一個方法來檢查屬性是否存在於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