We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Debugging Aspose Errors

Below is some code that works fine in general, however it's throwing an error on the attached document.

Is there any way to view the node tree of a document through Aspose.Words. We use Aspose & I often run into errors. I find it very hard to debug, since I don't know exactly why the error is happening. Below is one example. Is there any way for me to debug this on my own, or should I continue posting in the forums?

The following code throws an error on the attached document

  • private static string GetHtmlFromBookmark(string bookmarkName, Document doc, SaveFormat ThisSaveFormat)
  • {
  • Document docClone = doc.Clone();
  • Document doc1 = new Document();
  • Bookmark mark = docClone.Range.Bookmarks[bookmarkName];
  • Node node = mark.BookmarkStart.ParentNode;
  • Node endNode = mark.BookmarkEnd.ParentNode.NextSibling;
  • while (!node.Equals(endNode))
  • {
  • if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkStart))
  • {
  • Node child = (node as CompositeNode).FirstChild;
  • Node endChild = mark.BookmarkStart.NextSibling;
  • while (!child.Equals(endChild))
  • {
  • child = child.NextSibling;
  • child.PreviousSibling.Remove();
  • }
  • }
  • if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkEnd))
  • {
  • Node child = mark.BookmarkEnd;
  • while (!child.Equals(child.ParentNode.LastChild))
  • {
  • child = child.NextSibling;
  • child.PreviousSibling.Remove();
  • }
  • child.Remove();
  • }
  • doc1.FirstSection.Body.AppendChild(doc1.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));
  • node = node.NextSibling;
  • if (node == null)
  • break;
  • }
  • MemoryStream stream = new MemoryStream();
  • doc1.Save(stream, ThisSaveFormat);
  • string html = Encoding.UTF8.GetString(stream.GetBuffer());
  • return html;
  • }

Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. I modified your code and now it should work fine:

private static string GetHtmlFromBookmark(string bookmarkName, Document doc, SaveFormat ThisSaveFormat)

{

Document docClone = doc.Clone();

Document doc1 = new Document();

Bookmark mark = docClone.Range.Bookmarks[bookmarkName];

Node node = mark.BookmarkStart.ParentNode;

Node endNode = mark.BookmarkEnd.ParentNode.NextSibling;

while (!node.Equals(endNode))

{

if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkStart))

{

Node child = (node as CompositeNode).FirstChild;

Node endChild = mark.BookmarkStart.NextSibling;

if (child != null && endChild != null)

{

while (!child.Equals(endChild))

{

child = child.NextSibling;

child.PreviousSibling.Remove();

}

}

}

if ((node as CompositeNode).ChildNodes.Contains(mark.BookmarkEnd))

{

Node child = mark.BookmarkEnd;

while (!child.Equals(child.ParentNode.LastChild))

{

child = child.NextSibling;

child.PreviousSibling.Remove();

}

child.Remove();

}

doc1.FirstSection.Body.AppendChild(doc1.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));

node = node.NextSibling;

if (node == null)

break;

}

MemoryStream stream = new MemoryStream();

doc1.Save(stream, ThisSaveFormat);

string html = Encoding.UTF8.GetString(stream.GetBuffer());

return html;

}

Best regards.