調用XamlReader.Load()的button1_Click中的(對象發件人,RoutedEventArgs E)下面的代碼時,我得到XamlParseException時:調用XamlParseException XamlReader.Load(的XMLReader)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using HTMLConverter;
using System.Windows.Markup;
namespace StackoverflowTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public static SqlConnection conn = new SqlConnection("Server=" + @".\SQLEXPRE" +
";Initial Catalog=Hukuk;" +
"Integrated Security=True;" +
"User ID=;" +
"Password=;");
public MainWindow()
{
InitializeComponent();
try
{
conn.Open();
}
catch (Exception ex)
{
MessageBox.Show("Error opening SQL Server connection\n" + ex.Message);
//Close();
}
}
public static FlowDocument SetRTF(string xamlString)
{
StringReader stringReader = new StringReader(xamlString);
System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
return XamlReader.Load(xmlReader) as FlowDocument;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
SqlCommand commProc = new SqlCommand("SELECT Name, Content from Document where ID=3219", MainWindow.conn);
string str;
FlowDocumentReader fdocr = new FlowDocumentReader();
SqlDataReader dr = commProc.ExecuteReader();
try
{
if (dr.Read())
{
byte[] bt = (byte[])dr["Content"];
str = Encoding.Default.GetString(bt);
str = HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(str, true);
fdocr.Document = MainWindow.SetRTF(str);
}
}
catch (Exception ex)
{
MessageBox.Show("An error occured while trying to view document\n" + ex.Message);
}
dr.Close();
dr.Dispose();
}
}
}
很不幸,不能張貼xamlstring我想通過,因爲它太大了..是否有任何其他方式向你展示它?
和異常詳情請看這裏:
System.Windows.Markup.XamlParseException occurred
Message='Add value to collection of type 'System.Windows.Documents.TableRowGroupCollection' threw an exception.' Line number '1' and line position '28143'.
Source=PresentationFramework
LineNumber=1
LinePosition=28143
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, Boolean skipJournaledProperties, Uri baseUri)
at System.Windows.Markup.XamlReader.Load(XamlReader xamlReader, ParserContext parserContext)
at System.Windows.Markup.XamlReader.Load(XmlReader reader, ParserContext parserContext, XamlParseMode parseMode)
at System.Windows.Markup.XamlReader.Load(XmlReader reader)
at WpfApplication1.MainWindow.SetRTF(String xamlString) in C:\Documents and Settings\nosirovkh\Рабочий стол\посл\WpfApplication1\WpfApplication1\WpfApplication1\MainWindow.xaml.cs:line 69
InnerException: System.ArgumentNullException
Message=Value cannot be null.
Parameter name: item
Source=PresentationFramework
ParamName=item
StackTrace:
at MS.Internal.Documents.TableTextElementCollectionInternal`2.Add(TElementType item)
at MS.Internal.Documents.ContentElementCollection`2.System.Collections.IList.Add(Object value)
at System.Windows.Documents.TableRowGroupCollection.System.Collections.IList.Add(Object value)
at System.Xaml.Schema.XamlTypeInvoker.AddToCollection(Object instance, Object item)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.Add(Object collection, XamlType collectionType, Object value, XamlType valueXamlType)
InnerException:
您的代碼不會爲我拋出任何異常。要麼是你的'...'中的東西造成的,要麼是完全不同的東西。嘗試創建一個簡單的應用程序,實際上可以再現問題並將其發佈到此處。 – svick 2012-02-04 13:27:23
@svick:我同意,創建一個簡單的repro應用程序是去這裏的方式。我不想看到產生錯誤的代碼,提到'行位置41430'。 – 2012-02-04 14:19:57
它看起來像你試圖添加null到'TableRowGroupCollection'中。如果您使用適當的縮進和換行符來格式化XAML,而不是一行中的全部(如我所假設的那樣),則應該能夠找到出現問題的特定行。如果您在此時發佈XAML,我們應該能夠縮小問題的範圍。 – 2012-02-04 14:37:09