2011-01-25 13 views
2

後でFlowDocumentとしてFlowDocumentReaderに読み込むことができる.xamlファイルに変換する必要があるデータのコレクションがあります。 Paragraphs、Runsを直接インスタンス化するのではなく、後でドキュメントを作成するxamlを生成します。私は段落のXElementsを作成し、データを反復処理XmlWriterを使用して名前空間を持つFlowDocument xamlを書き出す

を、など、InlineUIContainersを実行し、呼び出し、その後うまくおよび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を手と要素を変更した場合する:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 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(); 

答えて

2

XamlWriter代わりにXmlWriterを使用してみてください。あなたがXLinqを使用している場合

0

次のことを試してみてください:

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), 
          ...); 
関連する問題