XmlElement не находит узел C#
Пишу код читалку fb2 книг. Из списка данных о книге выводит только данные файла (вес, дату).
Смотрел по отладке descriptionNode
и bodyNode
выдают Null
. Подскажите в чем проблема?
public Book ReadBook(string filePath)
{
Book book = new Book();
XmlDocument doc = new XmlDocument();
doc.Load(filePath);
XmlElement root = doc.DocumentElement;
if (root != null)
{
XmlNode descriptionNode = root.SelectSingleNode("description");
if (descriptionNode != null)
{
Console.WriteLine("work =" + descriptionNode.InnerText);
XmlNode titleInfoNode = descriptionNode.SelectSingleNode("title-info");
if (titleInfoNode != null)
{
Console.WriteLine("work =" + titleInfoNode.InnerText);
book.Title = GetElementValue(titleInfoNode, "book-title");
book.Authors = GetAuthorName(titleInfoNode.SelectSingleNode("author"));
book.Genre = GetElementValue(titleInfoNode, "genre");
book.Language = GetElementValue(titleInfoNode, "lang");
}
}
book.AddedDate = File.GetCreationTime(filePath);
book.Size = GetFileSizeInMB(filePath);
XmlNode bodyNode = root.SelectSingleNode("body");
if (bodyNode != null)
{
book.Content = bodyNode.InnerXml;
}
}
return book;
}
private string GetElementValue(XmlNode node, string elementName)
{
XmlNode element = node.SelectSingleNode(elementName);
return element?.InnerText ?? string.Empty;
}
private string GetAuthorName(XmlNode authorNode)
{
if (authorNode == null)
return string.Empty;
string firstName = GetElementValue(authorNode, "first-name");
string lastName = GetElementValue(authorNode, "last-name");
return $"{lastName} {firstName}".Trim();
}
private double GetFileSizeInMB(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return Math.Round(fileInfo.Length / 1048576.0, 2);
}
UPD: У меня есть другая версия кода Linq2xml:
public Book ReadBook(string filePath)
{
XDocument doc = XDocument.Load(filePath);
Book book = new Book();
book.Title = GetBookTitle(doc);
book.Authors = GetBookAuthors(doc);
book.Genre = GetBookGenre(doc);
book.Language = GetBookLanguage(doc);
book.AddedDate = File.GetCreationTime(filePath);
book.Size = GetFileSizeInMB(filePath);
book.Content = GetBookContent(doc);
return book;
}
private string GetBookTitle(XDocument doc)
{
XElement titleElement = doc.Root?.Element("description")?.Element("title-info")?.Element("book-title");
return titleElement?.Value ?? string.Empty;
}
private string GetBookAuthors(XDocument doc)
{
XElement authorElement = doc.Root?.Element("description")?.Element("title-info")?.Element("author")?.Element("first-name");
string firstName = authorElement?.Value ?? string.Empty;
authorElement = doc.Root?.Element("description")?.Element("title-info")?.Element("author")?.Element("last-name");
string lastName = authorElement?.Value ?? string.Empty;
//foreach
return $"{lastName} {firstName}".Trim();
}
private string GetBookGenre(XDocument doc)
{
XElement genreElement = doc.Root?.Element("description")?.Element("title-info")?.Element("genre");
//foreach
return genreElement?.Value ?? string.Empty;
}
private string GetBookLanguage(XDocument doc)
{
XElement langElement = doc.Root?.Element("description")?.Element("title-info")?.Element("lang");
return langElement?.Value ?? string.Empty;
}
private string GetBookContent(XDocument doc)
{
XElement bodyElement = doc.Root?.Element("body");
return bodyElement?.Value ?? string.Empty;
}
private double GetFileSizeInMB(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
return Math.Round(fileInfo.Length / 1048576.0, 2);
}
Ответы (1 шт):
Автор решения: RupKO
→ Ссылка
Не хватало пространства имен, как добавил все получилось
Разобрался с пространством имен добавил нескольско строк:
...
private readonly XNamespace fbNamespace = "http://www.gribuser.ru/xml/fictionbook/2.0";
public Book ReadBook(string filePath)
{
var namespaceManager = new XmlNamespaceManager(new NameTable());
namespaceManager.AddNamespace("fb", fbNamespace.ToString());
...
...
private string GetBookTitle(XDocument doc)
{
XElement titleElement = doc.Root?.Element(fbNamespace + "description")?.Element(fbNamespace + "title-info")?.Element(fbNamespace + "book-title");
return titleElement?.Value ?? string.Empty;
}
...