Hello
i have a coode that are trying to get the id of xml file where url exist.
My code
public static string GetPageIdByUrl(string url)
{
string folder = HttpContext.Current.Server.MapPath("/App_Data/Pages/" + Path.DirectorySeparatorChar);
foreach (string file in Directory.GetFiles(folder, "*.xml", SearchOption.TopDirectoryOnly))
{
var xml = XDocument.Load(file);
var q = from f in xml.Descendants("Page")
where (string)f.Element("Url").Value == url
select (string)f.Attribute("Id").Value;
if (q.Count() != 0)
{
return q.SingleOrDefault();
}
}
return null;
}
I get this error:
Object reference not set to an instance of an object. Line: select f.Attribute("Id").Value;
if "test" exist in a xml file <Url>test</Url>
i want it to return the attribute ID. But that sems only work for the first xml file.
Hope you get the problem.