.net 调用XamlReader.Load(xmlReader)时获取XamlParseException

.net 调用XamlReader.Load(xmlReader)时获取XamlParseException,.net,wpf,xaml,xamlparseexception,.net,Wpf,Xaml,Xamlparseexception,在按钮1中调用XamlReader.Load()时,我收到XamlParseException(对象发送器,RoutedEventArgs e)。请单击下面的代码: 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Windows; 使用System.Windows.Controls; 使用System.Windows.Data; 使用System.Windows.Documents; 使用System.Win

在按钮1中调用XamlReader.Load()时,我收到XamlParseException(对象发送器,RoutedEventArgs e)。请单击下面的代码:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用System.Data.SqlClient;
使用系统数据;
使用System.IO;
使用HTMLConverter;
使用System.Windows.Markup;
命名空间堆栈溢出测试
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
public static SqlConnection conn=new SqlConnection(“Server=“+@”。\SQLEXPRE”+
“初始目录=Hukuk;”+
“集成安全性=真;”+
“用户ID=;”+
“密码=;”;
公共主窗口()
{
初始化组件();
尝试
{
conn.Open();
}
捕获(例外情况除外)
{
MessageBox.Show(“打开SQL Server连接时出错\n”+ex.Message);
//Close();
}
}
公共静态流文档集RTF(字符串xamlString)
{
StringReader StringReader=新StringReader(xamlString);
System.Xml.XmlReader XmlReader=System.Xml.XmlReader.Create(stringReader);
将XamlReader.Load(xmlReader)作为FlowDocument返回;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
SqlCommand commProc=new SqlCommand(“从ID=3219的文档中选择名称、内容”,MainWindow.conn);
字符串str;
FlowDocumentReader fdocr=新的FlowDocumentReader();
SqlDataReader dr=commProc.ExecuteReader();
尝试
{
if(dr.Read())
{
字节[]bt=(字节[])dr[“内容”];
str=Encoding.Default.GetString(bt);
str=HTMLConverter.HtmlToXamlConverter.ConvertHtmlToXaml(str,true);
fdocr.Document=MainWindow.SetRTF(str);
}
}
捕获(例外情况除外)
{
MessageBox.Show(“试图查看文档时出错”\n“+ex.Message”);
}
Close博士();
dr.Dispose();
}
}
}
不幸的是,a无法发布我试图传递的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: 

看起来您的XAML可能是由某个程序生成的,这就是问题的根源

问题是:

<Table>
  <TableColumn Width="331"/>
  <TableColumn Width="48"/>
  <TableColumn Width="48"/>
  <TableColumn Width="48"/>
  <TableColumn Width="48"/>
  <TableColumn Width="44"/>
  <TableColumn Width="52"/>
  ...
</Table>

...
下面是正确的代码:

<Table>
  <Table.Columns>
    <TableColumn Width="331"/>
    <TableColumn Width="48"/>
    <TableColumn Width="48"/>
    <TableColumn Width="48"/>
    <TableColumn Width="48"/>
    <TableColumn Width="44"/>
    <TableColumn Width="52"/>
  </Table.Columns>
  ...
</Table>

...
出现这种情况的有6个表


我不知道是什么产生了这个XAML,但这似乎是一个坏错误。您需要修复它,或者自己修复XAML文件,或者编写一个小程序来查找此类内容,或者使用(最好是)Visual Studio手动进行修复。

使用以下XAML代码启动WPF应用程序时,我遇到了类似的错误:

<Window x:Class="XXX.Wpf.IV.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="IV" WindowState="Normal" Height="1024" Width="1280">

<Grid>
    <local:IVView x:Name="_ivView"/> 
</Grid>

</Window>


原来导致“XmalParseException添加到集合…”的原因是我的IVView类应该是UserControl,但我将其创建为Window的扩展。我怀疑我们有类似的问题。

您的代码没有为我带来任何异常。要么是由于您的
中的某些内容造成的,要么是完全不同的内容。试着创建一个简单的应用程序,实际复制问题,并将其发布到这里。@svick:我同意,创建一个简单的复制应用程序是一种方法。我不希望看到生成提到
行位置41430的错误的代码。看起来您正在尝试将
null
添加到
TableRowGroupCollection
中。如果您使用适当的缩进和换行来格式化XAML,而不是在一行上全部格式化(我假设是这样),那么您应该能够找到有问题的特定行。如果您在此时发布XAML,我们应该能够缩小问题的范围。@svick我创建了一个简单的应用程序,可以复制该问题。@kendfrey xamlstring值太大,无法在此处发布,我可以通过电子邮件或其他方式将其发送给您吗?@kendfrey您能详细告诉我您的意思吗“使用适当的缩进和换行符格式化XAML”?感谢您的回复!我正在尝试编写一个小程序,在必要的地方添加
。我将在完成后尽快发布结果。感谢您的回复,您帮助我解决了我的问题!