在C#中从Base64解码 [英] Decoding from Base64 in C#

查看:223
本文介绍了在C#中从Base64解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了XML文档并将此文档另存为

I created XML Document and saved this document as

  XmlDocument xmlDoc = new XmlDocument();
  XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
  xmlDoc.AppendChild(dec);
  XmlTextWriter writer = new XmlTextWriter(fullPath,Encoding.UTF8);
  writer.Formatting = Formatting.Indented;
  xMLDoc.Save(writer);
  writer.Flush();

然后我使用Base64 Encoder编码了这个文档

And then I have encoded this document using Base64 Encoder

解码器无法解析XML文件。
我自己创建了解码器并得到这个结果

The Decoder could not parse XML file. I created the decoder myself and got this result

 ?<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ClinicalDocument 
 xmlns=\"urn:hl7-org:v3\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
classCode=\"DOCCLIN\" moodCode=\"EVN\" schemaLocation=\"urn:hl7-org:v3
CDA.xsd\">\r\n  <typeId extension=\"POCD_HD000040\" root=\"2.16.840.1.113883.1.3
\" />\r\n

请帮我解决问题。
我如何保存XML文件以避免这些问题?
或者我如何编码到Base 64来解决问题?
我正在使用base64编码器来编码xml文件。
我正在请求文件。需要使用base64编码器。
我自己解码以检查问题在哪里。
解码器是Java。他们无法解析我认为的xml文件?在文件前面。

Please help me to resolve the issue. How I have to save the XML file to avoid the issues? Or How I have to encode to Base 64 to resolve the issue? I am using base64 encoder to encode xml file. I am requesting document. it is required to use base64 encoder. I decoded myself to check where is the problem. The decoder is Java . They can not parse the xml file I believe because ?< in front of the document.

推荐答案

这取决于你如何对它进行编码,但是你应该使用UTF-8文件被宣布为这样。以下是编码和解码的示例:

It depends on how you're encoding it, but you should use UTF-8 since the document is declared as such. Here are examples for encoding and decoding:

请见Jon Skeet的答案:_
C#base64编码/解码与序列化的对象发布

See Jon Skeet's answer here:
C# base64 encoding/decoding with serialization of objects issue

要编码:

public string EncodeStringToBase64(string stringToEncode)
{
    return Convert.ToBase64String(Encoding.UTF8.GetBytes(stringToEncode));
}

要解码:

public string DecodeStringFromBase64(string stringToDecode)
{       
    return Encoding.UTF8.GetString(Convert.FromBase64String(stringToDecode));
}

这篇关于在C#中从Base64解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆