getting pictures from word 2003 xml file

asish

Member
Joined
Jan 17, 2008
Messages
20
Programming Experience
Beginner
hi, here i parse & get z image as object & display it on the picture box...

VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim reader As XmlTextReader = New XmlTextReader("d:/document/images/test.xml")
        reader.WhitespaceHandling = WhitespaceHandling.None
        Dim body As Object = reader.NameTable.Add("body")
        Dim pict As Object = reader.NameTable.Add("pict")
        Dim len As Integer = 5000
        Dim b() As Byte
        Dim pic As Image
        While (reader.Read())
            If (pict = reader.LocalName And reader.NodeType = XmlNodeType.Element) Then
                reader.Read()
                reader.Skip()
                len = reader.ReadBase64(b, 0, 5000)
                Dim mem As System.IO.MemoryStream = New System.IO.MemoryStream()
                mem.Write(b, 0, len)
                mem.Position = 0
                pic = Image.FromStream(mem)
                PictureBox1.Image = pic
            End If
        End While
    End Sub

but z prob is zat i get error on zis line
len = reader.ReadBase64(b, 0, 5000)....null reference exception cud result at run time.....how can i solve this??!!!!:confused:
 
Last edited by a moderator:
sorry....

hey im really sorry... so here's the problem:
the code should get images from a word 2003 xml file and display it on a picture box each time the user clicks on a button...
everything looks to be fine except that i get error on this line:

len = reader.ReadBase64(b, 0, 5000)

it says: null reference exception could result at run time and "b" is underlined
how can i solve this??

here is the link from where i got the code:

http://blogs.msdn.com/kaevans/archive/2005/03/25/402426.aspx

thank you
 
modified code

While (reader.Read())
If (pict = reader.LocalName And reader.NodeType = XmlNodeType.Element) Then
reader.Read()
reader.Skip()
len = reader.ReadBase64(b, 0, 5000)
Dim mem As System.IO.MemoryStream = New System.IO.MemoryStream()
mem.Write(b, 0, len)
mem.Position = 0
pic = Image.FromStream(mem)

PictureBox1.Image = pic
pic.Save("d:document/images/" + CStr(count) + ".jpg")
count = count + 1
reader.Skip()
End If
End While

here is the modified code, what i want it to to is that it reads all the pictures for e.g. if it reads the 1st pict it stores is as 1.jpg and the 2nd picture as 2.jpg

the problem is that i get error...when ever there is 2 pictures in the xml document...how to solve this??....the first picture is saved but not the second picture.....

thks in advance....
 
the error message that i get....

An unhandled exception of type 'System.Xml.XmlException' occurred in System.Xml.dll

Additional information: '<v:imagedata src=' is not a valid Base64 text sequence. Line 150, position 97.


how to solve this error.....??
 
VB.NET:
Dim xdoc As New Xml.XmlDocument
xdoc.Load("Doc1.xml")
Dim nsmgr As New Xml.XmlNamespaceManager(xdoc.NameTable)
nsmgr.AddNamespace("w", "http://schemas.microsoft.com/office/word/2003/wordml")
For Each node As Xml.XmlNode In xdoc.SelectNodes("//w:pict/w:binData", nsmgr)
    Dim name As String = New Uri(node.Attributes("w:name").InnerText).Host
    My.Computer.FileSystem.WriteAllBytes(name, Convert.FromBase64String(node.InnerText), False)
Next
 
how to the bin data then?

thank you sir,

but your code allows me to get the filename of the pictures how do i get the pictures?...i have tried to find a solution using your code to find a solution but have been unsuccessful.
 
The posted code saves all images to files with the FileSystem.WriteAllBytes call.
 
changing the attribute name in the xml document

thank you for the reply....

but there is still one final problem...
so here's the xml file:


HTML:
<w:binData w:name="wordml://02000001.jpg">....</w:binData>
<v:shape id="_x0000_i1026" type="#_x0000_t75" style="width:308.25pt;height:68.25pt">
<v:imagedata src="wordml://02000002.jpg" o:title="banner"/></v:shape>


i wanted to how to change the <v:imagedata>.....scr attribute from
<v:imagedata src="wordml://02000002.jpg" to <v:imagedata src="images/02000002.jpg"

i.e. the v:image data attribute now extracts the pictures (that have been extracted from the bin node) stored in the folder images.
how this can be done?

VB.NET:
        Dim xdoc As New Xml.XmlDocument
        xdoc.Load("d:\document\test2.xml")
Dim nsmgrv As New Xml.XmlNamespaceManager(xdoc.NameTable)
        nsmgrv.AddNamespace("v", "urn:schemas-microsoft-com:vml")
        For Each node As Xml.XmlNode In xdoc.SelectNodes("//v:shape/v:imagedata", nsmgr)

is this how we proceed to reach the nodes?

please help. thank you.
 
Last edited by a moderator:
Yes, node.Attributes("w:name").InnerText is read/write, just save the doc afterwards.
 
Back
Top