XML을 문자열이 아닌 문서로 생각하십시오. "wrapper"라는 노드를 만들고 파일의 내용을 Base64로 인코딩 된 문자열로 저장합니다. 결과는 다음과 같습니다.
<ReturnValue>
<ErrorNumber>0</ErrorNumber>
<Message>my message</Message>
<wrapper type="bin.base64">PD94bWwgdmVyc2lvbj0iMS4wIj8+PHhzbDpzdHlsZXNoZWV0IHZ
lcnNpb249IjEuMCIgeG1sbnM6eHNsPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L1hTTC9UcmFuc2Zvcm0
iIHhtbG5zOm1zeHNsPSJ1cm46c2NoZW1hcy1taWNyb3NvZnQtY29tOnhzbHQiPjx4c2w6b3V0cHV0IG1
ldGhvZD0ieG1sIiAvPjx4c2w6dGVtcGxhdGUgbWF0Y2g9Ii8iPjwveHNsOnRlbXBsYXRlPjwveHNsOnN
0eWxlc2hlZXQ+</wrapper>
</ReturnValue>
다음 코드는 래퍼를 추가하고 콘텐츠를 인코딩하는 방법을 보여줍니다. 그런 다음 프로세스를 되돌려 모든 것이 작동 함을 보여줍니다.
XML에서 Base64를 사용하면 여러 가지 다른 응용 프로그램도 사용할 수 있습니다. 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);
}
}
}
중복 또는 새 네임 스페이스 선언은 정형식 오류가 발생하지 않아야하지만, 또한 당신의''요소 내부의 XML 선언을 삽입하는 것은 아마 원인이됩니다. 추가 XML 선언을 제거해도 문제가 해결되지 않으면 개체 직렬화 또는 웹 서비스 반환 값이 잘못된 XML을 생성합니다. –
jasso