2012-02-04 71 views
1

调用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: 
+0

您的代码不会为我抛出任何异常。要么是你的'...'中的东西造成的,要么是完全不同的东西。尝试创建一个简单的应用程序,实际上可以再现问题并将其发布到此处。 – svick 2012-02-04 13:27:23

+0

@svick:我同意,创建一个简单的repro应用程序是去这里的方式。我不想看到产生错误的代码,提到'行位置41430'。 – 2012-02-04 14:19:57

+0

它看起来像你试图添加null到'TableRowGroupCollection'中。如果您使用适当的缩进和换行符来格式化XAML,而不是一行中的全部(如我所假设的那样),则应该能够找到出现问题的特定行。如果您在此时发布XAML,我们应该能够缩小问题的范围。 – 2012-02-04 14:37:09

回答

2

它看起来像一些程序,这是问题的根源很可能产生你的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,但是这看起来像一个糟糕的bug。您可能需要修复它,或者自己修复XAML文件,或者编写一个小程序来找到这种东西,或者手动(最好)使用Visual Studio。

+0

感谢您的回应!我试图编写一个小程序在需要的地方添加''和''。我会尽快发布结果。 – 2012-02-07 12:28:49

+0

感谢所有回复,您帮我解决了我的问题! – 2012-02-10 06:20:06

0

我有一个类似的错误时,下面的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类应该是用户控件,但我创建它作为Window的扩展。我怀疑我们有类似的问题。