[英]Write to, and read binary data from XML file in .Net 2.0
I've ready many posts (such as this one) on SO now which explain how to write binary data to an XML using the XMLWriter.WriteBase64 method. 我已经在SO上准备了许多帖子(例如这一篇),它解释了如何使用XMLWriter.WriteBase64方法将二进制数据写入XML。 However, I've yet to see one which explains how to then read the base64'd data.
但是,我还没有看到一个解释如何读取base64'd数据的方法。 Is there another built in method?
还有其他内置方法吗? I'm also having a hard time finding solid documentation on the subject.
我也很难找到关于这个主题的可靠文档。
Here is the XML file that I'm using: 这是我正在使用的XML文件:
<?xml version="1.0"?>
<pstartdata>
<pdata>some data here</pdata>
emRyWVZMdFlRR0FFQUNoYUwzK2dRUGlBS1ZDTXdIREF ..... and much, much more.
</pstartdata>
The C# code which creates that file (.Net 4.0): 创建该文件的C#代码(.Net 4.0):
FileStream fs = new FileStream(Path.GetTempPath() + "file.xml", FileMode.Create);
System.Xml.XmlTextWriter w = new System.Xml.XmlTextWriter(fs, null);
w.Formatting = System.Xml.Formatting.None;
w.WriteStartDocument();
w.WriteStartElement("pstartdata");
#region Main Data
w.WriteElementString("pdata", "some data here");
// Write the binary data
w.WriteBase64(fileData[1], 0, fileData[1].Length);
#endregion (End) Main Data (End)
w.WriteEndDocument();
w.Flush();
fs.Close();
Now, for the real challenge... Alright, so as you all can see the above was written in .Net 4.0. 现在,对于真正的挑战......好吧,所以你们都可以看到上面是用.Net 4.0编写的。 Unfortunately, the XML file needs to be read by an application using .Net 2.0.
不幸的是,XML文件需要由使用.Net 2.0的应用程序读取。 Reading in the binary (base 64) data has proven to be quite the challenge.
读取二进制(base 64)数据已被证明是一项挑战。
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
xDoc.LoadXml(xml_data);
foreach (System.Xml.XmlNode node in xDoc.SelectNodes("pstartdata"))
{
foreach (System.Xml.XmlNode child in node.ChildNodes)
{
MessageBox.Show(child.InnerXml);
}
}
What would I need to add in order to read in the base 64'd data (shown above)? 为了读入基础64位数据(如上所示),我需要添加什么?
To read data from XML, you can use an XmlReader
, specifically the ReadContentAsBase64
method. 要从XML读取数据,可以使用
XmlReader
,特别是ReadContentAsBase64
方法。
Another alternative is to use Convert.FromBase64String
: 另一种方法是使用
Convert.FromBase64String
:
byte[] binaryData;
try
{
binaryData = System.Convert.FromBase64String(base64String);
}
Note: you should look at the code your are using for writing - it appears that you are writing the binary data into the pstartdata
element (as well as a pdata
element beforehand). 注意:您应该查看用于编写的代码 - 看起来您正在将二进制数据写入
pstartdata
元素(以及事先的pdata
元素)。 Doesn't look quite right - see the answer from @Yahia . 看起来不太对劲 - 请看@Yahia的答案 。
You seem to write some bad XML - use the following to write the data: 您似乎写了一些不好的XML - 使用以下内容来编写数据:
w.WriteStartDocument();
w.WriteStartElement("pstartdata");
#region Main Data
w.WriteElementString("pdata", "some data here");
// Write the binary data
w.WriteStartElement("bindata");
w.WriteBase64(fileData[1], 0, fileData[1].Length);
w.WriteEndElement();
#endregion (End) Main Data (End)
w.WriteEndDocument();
w.Flush();
fs.Close();
For reading you will have to use XmlReader.ReadContentAsBase64
. 要阅读,您必须使用
XmlReader.ReadContentAsBase64
。
As you have asked for other methods to write and read binary data - there are XmlTextWriter.WriteBinHex
and XmlReader.ReadContentAsBinHex
. 正如您要求其他方法来编写和读取二进制数据一样 - 有
XmlTextWriter.WriteBinHex
和XmlReader.ReadContentAsBinHex
。 BEWARE that these produce longer data than their Base64 pendants... 请注意,这些数据产生的数据比Base64吊灯更长......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.