How do I parse a non-GUI XAML file? -
ok, here's want do.
create "configuration file" using xaml 2009. this:
<tm:configuration xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:tm="clr-namespace:test.monkey;assembly=test.monkey" > <tm:configuration.targetfile>xxxx</tm:configuration.targetfile> <tm:configuration
parse file @ runtime in order object-graph.
easy way:
var z = system.windows.markup.xamlreader.parse(file.readalltext("xamlfile1.xaml")); (turns out support xaml 2009 after all.)
hard way, less dependencies:
var x = parsexaml(file.readalltext("xamlfile1.xaml")); public static object parsexaml(string xamlstring) { var reader = new xamlxmlreader(xmlreader.create(new stringreader(xamlstring))); var writer = new xamlobjectwriter(reader.schemacontext); while (reader.read()) { writer.writenode(reader); } return writer.result; }
creating xaml object graph:
public static string createxaml(object source) { var reader = new xamlobjectreader(source); var xamlstring = new stringwriter(); var writer = new xamlxmlwriter(xamlstring, reader.schemacontext); while (reader.read()) { writer.writenode(reader); } writer.close(); return xamlstring.tostring(); }
notes:
- fully qualify namespaces. has problem finding local assemblies namespace only.
- consider using contentpropertyattribute.
- useful notes on xaml 2009: http://wpftutorial.net/xaml2009.html
Comments
Post a Comment