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

发布于 2025-01-03 00:18:37 字数 4788 浏览 1 评论 0原文

在button1_Click(object sender, 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: 

I am getting XamlParseException when calling XamlReader.Load() in button1_Click(object sender, RoutedEventArgs e) the code below:

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();
        }
    }
}

Unfortunately a cannot post the xamlstring I'm trying to pass because it is too big..Is there any other way to show it to you?

And the exception details are here:

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: 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2025-01-10 00:18:37

看来您的 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 手动修复。

It looks like your XAML is probably generated by some program, which is the source of the problem.

Here is the problem:

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

And here is the correct code:

<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>

There are 6 tables where this occurs.

I don't know what generated this XAML, but this seems like a bad bug. You will either need it to be fixed or fix the XAML file yourself, either by writing a small program to find that kind of thing, or manually with (preferably) Visual Studio.

归属感 2025-01-10 00:18:37

当使用以下 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 的扩展。我怀疑我们也有类似的问题。

I had a similar error when launch a WPF application with the following xaml code:

<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>

Turns out the reason that caused "XmalParseException add to collection..." was that my IVView class should have been UserControl but I created it as an extension of Window. I am suspecting that we have having similar issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文