使用 XmlWriter 写出带有命名空间的 FlowDocument xaml

发布于 2024-10-14 08:48:16 字数 1887 浏览 1 评论 0原文

我有一组数据需要转换为 .xaml 文件,稍后可以将其作为 FlowDocument 加载到 FlowDocumentReader 中。我不直接实例化 Paragraphs、Runs,而是生成 xaml 以稍后创建文档。

我尝试过的:

我迭代数据,为段落、运行、InlineUIContainers 等创建 XElement,并构建 FlowDocument 结构,然后调用:

XmlWriter writer = XmlWriter.Create("output.xaml");
flowDocElem.WriteTo(writer);
writer.Close();

在使用应用程序中,我这样做:

flowDocument = XamlReader.Load(xamlFile) as FlowDocument;
flowDocumentReader.Document = flowDocument;
xamlFile.Close();

但是加载失败,因为它不知道 FlowDocument 是什么。 FlowDocument 元素看起来像这样:(

<FlowDocument Name="testDoc">

没有命名空间来说明读入 FlowDocument 时的含义。)

如果我手动编辑 .xaml 并将元素修改为:

Name="testDoc">

然后它就会加载得很好。

为 FlowDocument 创建 XElement 时,我尝试这样做:

new XElement("FlowDocument", new XAttribute("Name", "testDoc"), new XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"));

但这也不起作用 - 如果我尝试创建名称空间属性,则会出现错误。

我可以完全欺骗并将 xmlns 填充到元素中,然后调用类似的东西

File.WriteAllText("output.xaml", fixedTxt);

,但这感觉很脏,所以我认为我只是做错了。

想法?


更新:

虽然这可能不是问题的规范解决方案,但它确实有效:

通过向 XamlReader 添加 ParserContext,我能够解决加载 FlowDocument xml 的问题。

FileStream xamlFile = new FileStream("output.xaml", FileMode.Open, FileAccess.Read);
XamlReader x = new XamlReader();
ParserContext parserContext = new ParserContext();
parserContext.XmlnsDictionary.Add("","http://schemas.microsoft.com/winfx/2006/xaml/presentation");
flowDocument = XamlReader.Load(xamlFile, parserContext) as FlowDocument;
flowDocumentReader.Document = flowDocument;
xamlFile.Close();

I've got a collection of data that needs to be converted to a .xaml file that can later be loaded as a FlowDocument into a FlowDocumentReader. I don't directly instantiate Paragraphs, Runs, rather I generate the xaml to create the document later.

What I've tried:

I iterate through the data, creating XElements for Paragraphs, Runs, InlineUIContainers, etc. and build up the FlowDocument structure just fine and then call:

XmlWriter writer = XmlWriter.Create("output.xaml");
flowDocElem.WriteTo(writer);
writer.Close();

In the consuming app, I do this:

flowDocument = XamlReader.Load(xamlFile) as FlowDocument;
flowDocumentReader.Document = flowDocument;
xamlFile.Close();

But the loading fails because it doesn't know what a FlowDocument is. The FlowDocument element looks like so:

<FlowDocument Name="testDoc">

(There's no namespace there to shed light as to what a FlowDocument is when it is read in.)

If I hand edit the .xaml and modify the element to be:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Name="testDoc">

Then it'll load just fine.

When creating the XElement for the FlowDocument, I've tried to do this:

new XElement("FlowDocument", new XAttribute("Name", "testDoc"), new XAttribute("xmlns", "http://schemas.microsoft.com/winfx/2006/xaml/presentation"));

but that doesn't work either - gives me an error if I try to create the namespace attribute.

I can completely cheat and stuff that xmlns into the element and then call something like

File.WriteAllText("output.xaml", fixedTxt);

but that feels dirty and so I think I'm just plain doing it wrong.

Thoughts?


Update:

While this probably isn't the prescriptive solution to the problem, it does work:

By adding a ParserContext to the XamlReader, I was able to get past the problem with loading the FlowDocument xml.

FileStream xamlFile = new FileStream("output.xaml", FileMode.Open, FileAccess.Read);
XamlReader x = new XamlReader();
ParserContext parserContext = new ParserContext();
parserContext.XmlnsDictionary.Add("","http://schemas.microsoft.com/winfx/2006/xaml/presentation");
flowDocument = XamlReader.Load(xamlFile, parserContext) as FlowDocument;
flowDocumentReader.Document = flowDocument;
xamlFile.Close();

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

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

发布评论

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

评论(2

画中仙 2024-10-21 08:48:16

尝试使用 XamlWriter 而不是 XmlWriter

Try using XamlWriter instead of XmlWriter.

等待我真够勒 2024-10-21 08:48:16

如果您使用 XLinq,您应该尝试以下操作:

XNamespace ns = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xns = @"http://schemas.microsoft.com/winfx/2006/xaml";
XElement someElement = new XElement(ns + "FlowDocument",
                           new XAttribute(xns + "Name", name),
                           ...);

If you use XLinq you should try the following:

XNamespace ns = @"http://schemas.microsoft.com/winfx/2006/xaml/presentation";
XNamespace xns = @"http://schemas.microsoft.com/winfx/2006/xaml";
XElement someElement = new XElement(ns + "FlowDocument",
                           new XAttribute(xns + "Name", name),
                           ...);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文