Ho creato un metodo che controlla se esiste un attributo in un file XML. Se non esiste restituisce "False". Funziona ma ci vuole molto tempo per analizzare il file. Sembra che legga l'intero file per ogni singola riga. Ho perso qualcosa qui? Posso renderlo più efficace in qualche modo?Controllo XML parse se esiste l'attributo
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";
}
}
ho provato a sostituire il metodo con uno che solo i rendimenti e ricevere stringa:
public static string checkXMLcount(string x)
{
Console.WriteLine(x);
Console.ReadLine();
return x;
}
Ho fatto un file XML con una sola singola riga. La console stampa il valore 15 volte. Qualche idea?
Perché scrivere la propria versione di XPath, mi chiedo? – raina77ow