在 .Net 2.0 中写入和读取 XML 文件中的二进制数据

发布于 01-02 21:28 字数 1762 浏览 3 评论 0原文

我已经准备了很多关于 SO 的帖子(例如这个),它们解释了如何使用以下命令将二进制数据写入 XML XMLWriter.WriteBase64 方法。但是,我还没有看到解释如何读取 Base64 数据的文章。还有其他内置方法吗?我也很难找到有关该主题的可靠文档。

这是我正在使用的 XML 文件:

<?xml version="1.0"?>
<pstartdata>
  <pdata>some data here</pdata>
  emRyWVZMdFlRR0FFQUNoYUwzK2dRUGlBS1ZDTXdIREF ..... and much, much more.
</pstartdata>

创建该文件的 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();

现在,迎接真正的挑战... 好吧,正如大家所看到的,上面是用 .Net 4.0 编写的。不幸的是,XML 文件需要由使用 .Net 2.0 的应用程序读取。事实证明,读取二进制(基数 64)数据是一项相当大的挑战。

读取 XML 数据的代码 (.Net 2.0):

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);
                }
            }

我需要添加什么才能读取基本 64 位数据(如上所示)?

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. However, I've yet to see one which explains how to then read the base64'd data. 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 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):

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. Unfortunately, the XML file needs to be read by an application using .Net 2.0. Reading in the binary (base 64) data has proven to be quite the challenge.

Code to read in XML data (.Net 2.0):

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)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天生の放荡2025-01-09 21:28:05

要从 XML 读取数据,您可以使用 XmlReader,特别是 ReadContentAsBase64 方法。

另一种选择是使用 Convert.FromBase64String

byte[] binaryData;
try 
{
  binaryData = System.Convert.FromBase64String(base64String);
}

注意:您应该查看您用于编写的代码 - 看来您正在将二进制数据写入 pstartdata 元素(以及之前的 pdata 元素)。看起来不太正确 - 请参阅@Yahia 的答案

To read data from XML, you can use an XmlReader, specifically the ReadContentAsBase64 method.

Another alternative is to use 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). Doesn't look quite right - see the answer from @Yahia.

江湖正好2025-01-09 21:28:05

您似乎编写了一些错误的 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();

为了阅读,您必须使用 XmlReader.ReadContentAsBase64

正如您所要求的其他方法来写入和读取二进制数据 - 有 XmlTextWriter.WriteBinHexXmlReader.ReadContentAsBinHex。请注意,这些生成的数据比其 Base64 吊坠更长......

You seem to write some bad XML - use the following to write the data:

        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.

As you have asked for other methods to write and read binary data - there are XmlTextWriter.WriteBinHex and XmlReader.ReadContentAsBinHex. BEWARE that these produce longer data than their Base64 pendants...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文