2012-02-04 3 views
1

(XmlReaderの)XamlReader.Loadを呼び出すときのbutton1_Clickで)XamlParseExceptionを取得する(オブジェクト送信者、RoutedEventArgs e)に以下のコード:はXamlReader.Loadを呼び出すときに、私は(XamlParseExceptionを取得しています

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

+0

@スヴィック:シンプルなレトロアプリケーションを作成することは、ここに行く方法です。 'line position 41430'というエラーを生成するコードは見たくありません。 –

+0

'TableRowGroupCollection'に' null'を追加しようとしているようです。適切な字下げと改行でXAMLを書式化して、それが1行にあると仮定すると、問題のある特定の行を見つけることができるはずです。その時点でXAMLを投稿すると、問題を絞り込むことができるはずです。 –

答えて

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が何を生成したのか分かりませんが、これは悪いバグのようです。 XAMLファイルを修正したり、XAMLファイルを修正する必要があります。そのようなものを見つけるための小さなプログラムを作成するか、(できれば)Visual Studioを使用して手動で修正します。

+0

応答に感謝します!私は ''と ''を必要に応じて追加するための小さなプログラムを書こうとしています。私が終了するとすぐに結果を掲載します。 –

+0

すべての回答をありがとう、私の問題を解決するのを手伝った! –

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クラスがされている必要がありますということでしたUserControlが、私はそれをWindowの拡張として作成しました。私も同様の問題があると思われます。

関連する問題