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;
}
1行だけのXMLファイルを作成しました。コンソールは値を15回出力します。何か案は?