使用ReadContentAsBinHexl读取XML元素 [英] Read XML element using ReadContentAsBinHexl
问题描述
大家好!
大问题,小问题.
所以我正在使用xml.我创建了xml,其中一个元素是字节数组.
我将图像转换为byte []. Vs2010 create .xml我打开它,并且一切正常. A有一个长字节数组(〜30000个字符).
代码片段-> (项目是结构)
Hi guys!
Big problem, small question.
So I''m working with xml. I create xml and one of the element is byte array.
I convert image to byte[]. Vs2010 create .xml I open it and everyting if fine. A have a long byte array (~30000 char).
Code snipet -> (item is struc)
xmlWriter.WriteStartElement("PictureByteArray");
byte[] bytePicArray = imageToByteArray(item.picture);
xmlWriter.WriteBinHex(bytePicArray, 0, bytePicArray.Length);
private byte[] imageToByteArray(Image img)
{
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
return ms.ToArray();
}
}
问题是我现在如何才能从保存的xml文件中读取该字节数组????
我尝试使用->
XmlReader.ReadContentAsBinHex方法
但没有运气.我不明白有人可以给我一些建议吗???
谢谢. :)
The problem is How can I now read from saved xml file that byte array????
I trying to use ->
XmlReader.ReadContentAsBinHex Method
but no luck. I don''t understand. Can someone give me some advice???
Thanks. :)
推荐答案
为什么在XmlWriter
/XmlReader
的较低级别上工作?
您可以更好地使用数据合同.
参见:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].
我在过去的解决方案中主张采用这种出色的方法.请参阅:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [
Why working at the low level ofXmlWriter
/XmlReader
?
You can better of using Data Contract.
See:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].
I advocated this great approach in my past solutions. Please see:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating a property files...[^].
—SA
我会将字节数组转换为Base64,然后将base64字符串序列化为xml,就像这样
I would convert my byte array into Base64 and then serialize the base64 string to xml, something like this
void SavePic()
{
Image img = Image.FromFile("ID001.jpg");
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] picArray = ms.ToArray();
string picString = Convert.ToBase64String(picArray);
XmlWriter writer = XmlWriter.Create("pic.xml");
writer.WriteStartDocument();
writer.WriteStartElement("pic");
writer.WriteBase64(picArray,0, picArray.Length);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
}
然后要检索图像,您可以执行以下操作(Convert.FromBase64String
在这里是可操作的)
then to retrieve the image you do something like this (Convert.FromBase64String
being the operative here)
void ViewPic()
{
var x = XDocument.Load("pic.xml");
string s = (string)x.Descendants("pic").First();
byte[] imageBytes = Convert.FromBase64String(s);
MemoryStream ms = new MemoryStream(imageBytes);
pictureBox1.Image = Image.FromStream(ms, true);
}
希望这对您有帮助
Hope this helps
您好亲爱的
一段时间以来,我正在研究WPF-WCF聊天应用程序,我想将用户信息保存在服务器端的XML文件中.用户信息包括用户名,用户系列,用户图片等.我最简单的方法是将数据保存和检索到XML文件中:使用DataSet和DataTable.
数据集在XML上有一些有用的方法形成形式
Hello Dear
some time a go I was working on A WPF-WCF Chat application , i wanted to save user information in a XML File at server side . user information includes user name , user family , user image and etc . to save and retrieve these information in a XML file i did easiest way : using DataSet and DataTable .
dataset has some usefull method formworking on XML
DataSet Dset = new DataSet();
Dset.ReadXml("FilePath");
Dset.ReadXmlSchema("FilePath");
Dset.WriteXml("FilePath");
Dset.WriteXmlSchema("FilePath");
DataTable Dtable = new DataTable();
Dtable.ReadXml("FilePath");
Dtable.ReadXmlSchema("FilePath");
Dtable.WriteXml("FilePath");
Dtable.WriteXmlSchema("FilePath");
我可以轻松地从XML文件保存和检索包括图像文件在内的数据
使用这种方法,就可以了
祝你好运
i could save and retrieve data including image file , from XML file easily
use this methods , it works
Good Luck
这篇关于使用ReadContentAsBinHexl读取XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!