public static List <WPCategory> ImportCategories(string fileLocation) { try { var nsm = new XmlNamespaceManager(new NameTable()); nsm.AddNamespace("wp", "http://wordpress.org/export/1.2/excerpt/"); var parseContext = new XmlParserContext(null, nsm, null, XmlSpace.Default); using (var reader = XmlReader.Create(fileLocation, null, parseContext)) { var doc = XDocument.Load(reader); string WordpressNamespace = "http://wordpress.org/export/1.2/"; XNamespace wpContent = WordpressNamespace; List <WPCategory> blogCategories = new List <WPCategory>(); foreach (XElement element in doc.Descendants("channel")) { foreach (XElement e in element.Descendants(wpContent + "category")) { WPCategory cat = new WPCategory(e); blogCategories.Add(cat); } } return(blogCategories); } } catch (Exception ex) { Log.Error("Failed to import categories: " + ex.Message, "WP Importer"); return(new List <WPCategory>()); } }
public static IEnumerable <WPCategory> GetPostCategories( this XElement @this) { var categoryNodes = @this.Elements( XName.Get("category")); foreach (var categoryNode in categoryNodes) { var domainAttribute = categoryNode.Attribute(XName.Get("domain")); if (domainAttribute == null) { throw new XmlException( $"This \"category\" node has no \"domain\" attribute."); } if (domainAttribute.Value != "category") { continue; } var nicenameAttribute = categoryNode.Attribute(XName.Get("nicename")); var categoryName = categoryNode.Value; var cultureWarsCategory = WPCategory.FromNameOrNull(categoryName); if (cultureWarsCategory == null) { if (nicenameAttribute == null) { throw new XmlException( $"This \"category\" node has no \"nicename\" attribute."); } cultureWarsCategory = new WPCategory( categoryName, nicenameAttribute.Value); } yield return(cultureWarsCategory); } }