我有一个非常奇怪的空引用异常,它阻止了我的进展。
我正在制作一本书。每本书都有页面,每个页面都可以有文本项和图像。我试图将这些对象保存在Xml文件中。 我第一次保存对象一切顺利。 Xml由c#创建,加载,填充并保存。当我尝试向书中添加/保存新页面时,我无法获取已存在的Xml文件。问题是我的代码永远不会达到我可以读取文件的程度。当我创建一个空的XDocument对象时,它会出错。错误说" documentType是一个空引用"。
第一次调用create方法是我保存第一页的第一组对象。我没有收到任何错误,Xml被正确保存。但是当我再次尝试保存(新页面或当前页面)时,我收到错误(图片底部的帖子)。
调试器在XDocument doc之后停止; (在LoadXml中)然后抛出图像中显示的NullReferenceException。 通过向XML添加XDocumentType节点来解决此问题。
新错误: 新错误表明XDocument的Root是一个空引用。在DocumentType给出错误之前。
加载Xml
/// <summary>
/// Loads the XML file for the given path.
/// A new document will be created if the file does not exist or is corrupted.
/// </summary>
/// <param name="collectionId">The collectionId to load the file from.</param>
/// <param name="newFile">A boolean result which states if the returned file is new.</param>
/// <returns>The requested file or a new one.</returns>
public static XDocument LoadXml(int collectionId, int version, out bool newFile, CollectionType collectionType)
{
newFile = false;
XDocument doc; // Here I get the error when I run the code for a second time
string file = GetXmlPath(collectionId, collectionType, version) + "/photobook.xml";
if (System.IO.File.Exists(file))
{
try
{
doc = XDocument.Load(file);
}
catch
{
newFile = true;
doc = CreateNewXml();
}
}
else
{
newFile = true;
doc = CreateNewXml();
}
return doc;
}
SaveNewPage
/// <summary>
/// Add and save a new page to an existing book.
/// </summary>
/// <param name="pageInfoParam">New pageInfo object that has to be added to the collection.</param>
/// <param name="collectionId">Id of the collection which is being edited.</param>
/// <param name="version">Version of the collection which is being edited.</param>
/// <returns>True if succesful.</returns>
public static bool SaveNewPageInfo(PageInfo pageInfoParam, int collectionId, int version)
{
bool result = false;
// Make sure the collection exists
if (!CollectionExists(collectionId, version))
{
Logger.log("Error: Collection {0} not found.", collectionId);
throw new Exception("Fotoboek niet gevonden.");
}
Logger.log("Saving page info...");
bool newFile;
XDocument doc = XmlService.LoadXml(collectionId, version, out newFile, CollectionType.collection);
if (!newFile)
{
// This should never happen, we just successfully saved the XML
result = false;
Logger.log("Xml could not be loaded when trying to save a new page.");
throw new Exception("Fout bij het opslaan van de nieuwe pagina.");
}
// Get all current pages from the document.
List<XElement> pages = doc.Root.Element("inner").Elements("page").ToList();
#region New page in new book
// If no pages available we have to add a new page element.
#endregion
#region Edit existing page in book
// If pages exist find the page we edited, remove all text and add the new text.
#endregion
// Try to save the XML file
if (!XmlService.SaveXml(doc, collectionId, version))
{
Logger.log("Xml could not be saved.");
throw new Exception("De pagina kon niet worden opgeslagen.");
}
else
{
Logger.log("Page info for collection {0} saved successfully in Xml.", collectionId);
result = true;
return result;
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Book SYSTEM "Book.dtd">
<album version="0.2" settingsVersion="1">
<size />
<cover />
<inner>
<page>
<pagenumber>1</pagenumber>
<image>04.jpg</image>
<textelements>
<text>
<value>Test </value>
<font>arial</font>
<size>18pt</size>
<style>normal</style>
<weight>normal</weight>
<color>#77DD44</color>
<rotation>0</rotation>
<alignment>start</alignment>
<position>
<x>50</x>
<y>50</y>
</position>
</text>
</textelements>
</page>
</inner>
</album>