Skip to content

Instantly share code, notes, and snippets.

@TinkerWorX
Created February 15, 2018 14:52
Show Gist options
  • Save TinkerWorX/188d5617491c607c4f412b6f91dc8038 to your computer and use it in GitHub Desktop.
Save TinkerWorX/188d5617491c607c4f412b6f91dc8038 to your computer and use it in GitHub Desktop.
<Application.Resources>
<loaders:XmlResourceLoader x:Key="GlobalProductClassifications" Path="data/external/Custom_GPCList.xml" Type="{x:Type gs1:Schema}" />
</Application.Resources>
<ListBox ItemsSource="{Binding Segments, Source={StaticResource GlobalProductClassifications}}" />
public class XmlResourceLoader : MarkupExtension
{
public string Path { get; set; }
public Type Type { get; set; }
public object Value { get; private set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (this.Value != null)
return this.Value;
try
{
using (var stream = File.OpenText(this.Path))
this.Value = new XmlSerializer(this.Type).Deserialize(stream);
}
catch (Exception e)
{
Log.Error(e, "Error while deserializing '{path'} into '{type}'.", this.Path, this.Type);
}
return this.Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment