static public SlideshowModel ParseFile(string filename)
{
var ssModel = new SlideshowModel();
var doc = XDocument.Parse(File.ReadAllText(filename));
if (!doc.Root.Name.LocalName.Equals(XmlRootName))
{
throw new Exception(string.Format("Wrong namespace '{0}', expected {1}", doc.Root.Name.LocalName,XmlRootName));
}
ssModel.Filename = filename;
ssModel.Name = doc.Root.GetAttribute(XmlAttrName, "");
ssModel.SlideSeconds = doc.Root.GetAttribute(XmlAttrSlideDuration, ssModel.SlideSeconds);
ssModel.TransitionSeconds = doc.Root.GetAttribute(XmlAttrTransitionDuration, ssModel.TransitionSeconds);
ssModel.FindAPhotoHost = doc.Root.GetAttribute(XmlAttrFindAPhotoHost, ssModel.FindAPhotoHost);
foreach (var a in doc.Root.Attributes())
{
if (!expectedAttributes.Contains(a.Name.LocalName))
{
throw new Exception(string.Format("Unexpected attribute: {0}", a.Name.LocalName));
}
}
foreach (var f in doc.Descendants("folder"))
{
ssModel.FolderList.Add(FolderModel.FromElement(f));
}
foreach (var s in doc.Descendants("search"))
{
ssModel.Search = s.GetAttribute("query", null);
}
if (ssModel.FolderList.Count < 1 && ssModel.Search == null)
{
throw new Exception("Missing either 'folder' or 'search' tag");
}
foreach (var e in doc.Root.Elements())
{
if (!expectedElements.Contains(e.Name.LocalName))
{
throw new Exception(string.Format("Unexpected element: {0}", e.Name.LocalName));
}
}
return ssModel;
}