Extract a single file without path

Jul 27, 2011 at 9:23 PM

Hello,

I have a problem to extract a specific file. I try to find a file with zipfile.SelectEntries, this works fine. The problem is, the file is stored within the archive with a path, but I need only the file (it is a XML-File) to open as a xmldocument to read out a tag.

How can I extract/open the specific xml into a system.xml.xmldocument?

I can't find a solution for my problem :-(

Kind regards,
Maximilian

Coordinator
Jul 28, 2011 at 1:01 AM

Ok, so it seems to me you don't actually want the xml file, but you want to read an element from the file.

In this case I'd suggest you call ZipEntry.OpenReader(), which gives you a readable stream for the entry in question.  You can then use the classes in the System.Xml namespace, like System.Xml.XmlReader, to read the xml data. Call the XmlReader.Create() method passing the stream you received from ZipEntry.OpenReader(), and you should be able to read that XML data.

The path in the ZipEntry is used only if you call ZipEntry.Extract(). Calling OpenReader() means you sidestep the path issue.

If you actually DO want to extract the file, then you want to set the FileName property on the ZipEntry before calling Extract().   Specify anything you like for the FileName; if you want a file in the local directory, then you could do myEntry.FileName = System.IO.Path.GetFileName(myEntry.FileName);

Jul 28, 2011 at 8:03 AM

Hi Cheeso,

thank you very much for your information, so I have changed my code to test a possibility, but now I get an exeption and I don't know why.

Here is my test code:

Dim ironCount = ironFile.SelectEntries("TestInfo.xml")
ListBox1.Items.Add("Found entries: " & ironCount.Count)

For Each ironEntries In ironCount
    ListBox1.Items.Add(ironEntries.FileName)

    If ironEntries.UsesEncryption Then
        ironEntries.Password = ZipPasswort
    End If

    Dim ironXmlReader As System.Xml.XmlReader = Xml.XmlReader.Create(ironEntries.OpenReader)

    Dim ironXmlDoc As New System.Xml.XmlDocument
    ironXmlDoc.LoadXml(ironXmlReader.Read)
    ...
Next
 
An exeption occured on "ironXmlDoc.LoadXml..." => Data at the root level is invalid. Line 1, position 1.
 
Now I have tested with a xmlstring:
Dim xmlstring As String = "<?xml version=""1.0"" ?><NewNode><SecondNode><LastEntry>4711</LastEntry></SecondNode></NewNode>"
ironXmlDoc.LoadXml(xmlstring)

In this case I can read the Entry for <LastEntry>, but not if I read the same content within the file.
I'm not sure where is my issue and I'm a little confused.
Can anybody tell me the correct way to read out a XML-Tag within a xml file which is stored into an archive file?
Kind regerads,
Maximilian
Coordinator
Jul 29, 2011 at 6:30 AM

What is coming out of the stream returned by OpenReader()?  are you sure it is valid XML?

To diagnose this, I suggest that you store the result of the OpenReader into a stream variable, then call Read() on that stream, into a buffer.  Print out or otherwise display the contents of what you can read from that stream. If you see something that is not an XML document, then that would explain the "Data at the root level is invalid" error message.

It could be a text encoding issue, for example.  Or it could be that it is simply not an XML file.  

Jul 30, 2011 at 8:56 PM
Edited Jul 30, 2011 at 8:59 PM

Hi Cheeso,

thank you very much, with your information I have solved the problem:

Dim ironXmlReader As New System.IO.StreamReader(Entry.OpenReader("VeryGoodPassword"))
Dim ironXmlDocument As New System.Xml.XmlDocument
ironXmlDocument.LoadXml(ironXmlReader.ReadToEnd)
ironXmlReader.Close()

I don't know why the XmlReader not worked, but now I get the result what I need.

Kind regards,
Maximilian

 

Coordinator
Jul 30, 2011 at 9:14 PM

Ah, I'm glad to hear it.