Hãy nghĩ XML là tài liệu không phải là chuỗi. Tạo một nút có tên là "wrapper" và lưu trữ nội dung tệp của bạn trong đó dưới dạng chuỗi được mã hóa Base64. Kết quả sẽ trông như thế này.
<ReturnValue>
<ErrorNumber>0</ErrorNumber>
<Message>my message</Message>
<wrapper type="bin.base64">PD94bWwgdmVyc2lvbj0iMS4wIj8+PHhzbDpzdHlsZXNoZWV0IHZ
lcnNpb249IjEuMCIgeG1sbnM6eHNsPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L1hTTC9UcmFuc2Zvcm0
iIHhtbG5zOm1zeHNsPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOnhzbHQiPjx4c2w6b3V0cHV0IG1
ldGhvZD0ieG1sIiAvPjx4c2w6dGVtcGxhdGUgbWF0Y2g9Ii8iPjwveHNsOnRlbXBsYXRlPjwveHNsOnN
0eWxlc2hlZXQ+</wrapper>
</ReturnValue>
Mã sau đây cho biết cách thêm trình bao bọc, mã hóa nội dung. Sau đó, nó đảo ngược quá trình để cho thấy rằng tất cả "công trình".
Sử dụng Base64 trong XML cũng có một số ứng dụng khác. Ví dụ: nhúng hình ảnh hoặc các tài liệu khác trong nội dung XML.
using System;
using System.IO;
using System.Xml;
public class t
{
static public string EncodeTo64(string toEncode) {
byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
static public string DecodeFrom64(string encodedData) {
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
return returnValue;
}
public static void Main() {
try {
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.LoadXml(@"
<ReturnValue>
<ErrorNumber>0</ErrorNumber>
<Message>my message</Message>
</ReturnValue>
");
XmlNode nodeMessage = doc.SelectSingleNode("/ReturnValue/Message");
if(nodeMessage != null) {
XmlDocument docImport = new XmlDocument();
docImport.Load("docwithnamespace.xml");
// create a wrapper element for the file, then import and append it after <Message>
XmlElement nodeWrapper = (XmlElement)doc.CreateElement("wrapper");
nodeWrapper.SetAttribute("type", "bin.base64");
nodeWrapper = (XmlElement)doc.ImportNode(nodeWrapper, true);
XmlNode ndImport = nodeMessage.ParentNode.AppendChild(nodeWrapper.CloneNode(true));
ndImport.InnerText = EncodeTo64(docImport.OuterXml);
doc.Save("wrapperadded.xml");
// Next, let's test un-doing the wrapping
// Re-load the "wrapped" document
XmlDocument docSaved = new XmlDocument();
docSaved.Load("wrapperadded.xml");
// Get the wrapped element, decode from base64 write to disk
XmlNode node = doc.SelectSingleNode("/ReturnValue/wrapper");
if(node != null) {
// Load the content, and save as a new XML
XmlDocument docUnwrapped = new XmlDocument();
docUnwrapped.LoadXml(DecodeFrom64(node.InnerText));
docUnwrapped.Save("unwrapped.xml");
Console.WriteLine("Eureka");
}
}
} catch(Exception e) {
Console.WriteLine(e.Message);
}
}
}
Nguồn
2011-11-30 17:47:15
Khai báo không gian tên dự phòng hoặc mới trong dữ liệu được nhúng không được gây ra lỗi cấu thành tốt, nhưng nhúng cũng khai báo XML bên trong phần tử '' có thể sẽ gây ra điều đó. Nếu việc loại bỏ khai báo XML thừa không khắc phục được vấn đề, thì giá trị trả về của đối tượng hoặc dịch vụ web của bạn tạo ra XML không đúng định dạng. –
jasso