XmlTextWriter, XmlDocument

  • Thread starter Thread starter Ars Comm - Ciro Ferraiuolo
  • Start date Start date
A

Ars Comm - Ciro Ferraiuolo

Hi all, I need to embed an image to an existing xml file. I guess this is
possible using XmlTextWriter.WriteBase64() method. My problem is that
XmlTextWriter writes to a new document.
How can I edit an existing doc (for example with XmlDocument class) and
write an image to it?
I'm sorry but I'm a bit confused about the difference between XmlTextWriter
and XmlTextDocument.

Thanks.
 
Hello, Ars!

To put it simple with XmlDocument you're working with object model of the
XML file, XmlTextWriter is doesn't introduce object model.
Working with XmlTextWriter puts more work on you as a developer.

IMO with XmlDocument it is far more simple to edit xml documnet.

With XmlDocument getting to the necessary node will look like:

XmlDocument xmlDoc= new XmlDocument();
xmlDoc.Load(xmlFile);
XmlNode toWrite = xmlDoc.SelectSingleNode(xpathToNode);

now you can create attributes or create text section.
and then save the document
xmlDoc.Save(file);

Since XmlDocument builds in-memory object model of the XML file it consumes
more memory then XmlTextWriter.
If you have small xmldocument it is not an issue.

--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Wed, 17 Oct 2007 16:16:35 +0200:

ACC> Hi all, I need to embed an image to an existing xml file. I guess
ACC> this is possible using XmlTextWriter.WriteBase64() method. My
ACC> problem is that
ACC> XmlTextWriter writes to a new document.
ACC> How can I edit an existing doc (for example with XmlDocument class)
ACC> and write an image to it?
ACC> I'm sorry but I'm a bit confused about the difference between
ACC> XmlTextWriter and XmlTextDocument.

ACC> Thanks.
 
Ok, perfect. Let's say I use the
xmlDoc.AppendChild(xmlDoc.CreateElement(....))...How to put the image in?
 
Hello, Ciri!

Using Base64 format you can add that image as a text section to newly
created XmlElement object.

say,
byte[] image;
XmlElement xmlImage = xmlDoc.CreateElement("image");

XmlText imageText = xmlDoc.CreateText(Convert.ToBase64String(image));
xmlImage.AppendChild(imageText);

xmlDoc.AppendChild(xmlImage);

Note, that Convert class also has a method to get back byte array
Convert.FromBase64String(...);
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Wed, 17 Oct 2007 16:57:47 +0200:

C> Ok, perfect. Let's say I use the
C> xmlDoc.AppendChild(xmlDoc.CreateElement(....))...How to put the image
C> in?


ACC>>> Hi all, I need to embed an image to an existing xml file. I guess
ACC>>> this is possible using XmlTextWriter.WriteBase64() method. My
ACC>>> problem is that
ACC>>> XmlTextWriter writes to a new document.
ACC>>> How can I edit an existing doc (for example with XmlDocument
ACC>>> class)
ACC>>> and write an image to it?
ACC>>> I'm sorry but I'm a bit confused about the difference between
ACC>>> XmlTextWriter and XmlTextDocument.

ACC>>> Thanks.
 
Wow! That's it.
Thank you very much.

Vadym Stetsiak said:
Hello, Ciri!

Using Base64 format you can add that image as a text section to newly
created XmlElement object.

say,
byte[] image;
XmlElement xmlImage = xmlDoc.CreateElement("image");

XmlText imageText = xmlDoc.CreateText(Convert.ToBase64String(image));
xmlImage.AppendChild(imageText);

xmlDoc.AppendChild(xmlImage);

Note, that Convert class also has a method to get back byte array
Convert.FromBase64String(...);
--
With best regards, Vadym Stetsiak.
Blog: http://vadmyst.blogspot.com

You wrote on Wed, 17 Oct 2007 16:57:47 +0200:

C> Ok, perfect. Let's say I use the
C> xmlDoc.AppendChild(xmlDoc.CreateElement(....))...How to put the image
C> in?


ACC>>> Hi all, I need to embed an image to an existing xml file. I guess
ACC>>> this is possible using XmlTextWriter.WriteBase64() method. My
ACC>>> problem is that
ACC>>> XmlTextWriter writes to a new document.
ACC>>> How can I edit an existing doc (for example with XmlDocument
ACC>>> class)
ACC>>> and write an image to it?
ACC>>> I'm sorry but I'm a bit confused about the difference between
ACC>>> XmlTextWriter and XmlTextDocument.

ACC>>> Thanks.
 
Back
Top