Search code examples
c#xmlstreamxps

C# Read XML from fpage of XPS stream


I´d like to read the XML structure of an fpage Element in an XPS as Stream.

So to further explain what i want to do:

If you save a .xps you can rename it to a zip archive then you can read the xml e.g.
xps\Documents\1\Pages\1.fpage

Now I want to read this xml structure in my C# program.
Because save to disc, rename, navigate, extract, read, delete isn´t a good option i have this xps file already available as a stream in my program.
So the question is how can i access this XPS structure from stream and read it´s xml?


Solution

  • So if found the answer myself (isn´t that hard if you know how^^).

    You need System.IO.Packaging.Package and a Reference to ReachFramework then create a new XpsDocument and via XmlReader you can read the XML structure.

    Small sample for reading first page:

    XpsDocument xpsFromStream = new XpsDocument(Package.Open(stream)); //ReachFramework
    IXpsFixedDocumentSequenceReader fixedDocSeqReader = xpsFromStream.FixedDocumentSequenceReader;
    
    if (fixedDocSeqReader != null)
    {
        XmlReader pageContentReader = fixedDocSeqReader.FixedDocuments[0].FixedPages[0].XmlReader;
        //Sample:
        while (pageContentReader.Read())
        {
            if (pageContentReader.Name == "Path")
                //Do stuff
        } 
    }