none
XamlXmlReader and PackageStore RRS feed

  • Question

  • Hello,

    I'm trying to deserialize a Xaml file contained in a package. This Xaml file references other resources (images) stored in the same package.

    My deserialization code requires a node loop so I'm using XamlXmlReader / XamlObjectWriter pair.

    Here is deserializer's source (node loop stripped for simplicity):

            IDocumentPaginatorSource DeserializeDocumentFromXaml(Package SourcePackage, string FileName)
            {
                // search for stored document in the package
                PackageRelationshipCollection Relationships = SourcePackage.GetRelationshipsByType(SchemaTemplate);
                if (Relationships.Count() == 0)
                {
                    // no documents of needed type are present in the package
                    throw new FormatException(
                        string.Format("File {0} is in unsupported format.",
                            FileName)
                        );
                }
    
                // get access to file containing stored Xaml file
                Uri XamlUri = Relationships.First().TargetUri;
                PackagePart XamlDocument = SourcePackage.GetPart(XamlUri);
                using (Stream XamlStream = XamlDocument.GetStream())
                {
                    // prepare to decode Xaml, get pack Uri of package it's in and set it as Xaml's Base URI
                    Uri PackPackageUri = PackUriHelper.Create(new Uri(@"memorystream://test.zip")); // - using nonexistent path doesn't work
                    //Uri PackPackageUri = PackUriHelper.Create(new Uri(FileName)); // - using real path works
                    try
                    {
                        PackageStore.AddPackage(PackPackageUri, SourcePackage);
                        XamlXmlReaderSettings settings = new XamlXmlReaderSettings();
                        settings.BaseUri = PackPackageUri;
                        using (XamlXmlReader XmlReader = new XamlXmlReader(XamlStream, settings))
                        {
                            // prepare to create objects being decoded from Xaml file
                            using (XamlObjectWriter ObjectWriter = new XamlObjectWriter(XmlReader.SchemaContext))
                            {
                                // Xaml decoding node loop
                                while (XmlReader.Read())
                                {
                                    // example only - simple deserializer
                                    ObjectWriter.WriteNode(XmlReader);
                                }
                                return ObjectWriter.Result as IDocumentPaginatorSource;
                            }
                        }
                    }
                    finally
                    {
                        PackageStore.RemovePackage(PackPackageUri);
                    }
                }
            }
    

    Now comes the problem. Parameter "FileName" should be a hint used only to display reasonable error messages to the user. This file will not be directly available in the file system.

    So I should only access the package by memory stream.

    Accessing it from memory stream works fine for Xaml itself, but system can't find images stored in the package.

    As far as I understand from the documentation, such task should be solved by using PackageStore and attaching opened package to any Uri. Operations done of the package by it's Uri should then be directed to an already opened package without opening it for the second time.

    Unfortunatelly it doesn't work for me. Above code only works if I use real full file name in FileName parameter (so file is visible in file system) and if I use commented out fragment of code (to use real Uri of the package). If I try to use a non existent Uri and use PackageStore.AddPackage, system can't find images stored in the package.

    I have already tried to comment out PackageStore.RemovePackage to make sure I'm not removeing assignment too early. It doesn't change anything.

    Is there anything else needed to make PackageStore work?

    Monday, April 29, 2013 12:31 PM

All replies