2017-09-05 2 views
0

C#으로 XmlDoc을 만들고 Newtonsoft를 사용하여 JSON에 직렬화합니다. 그것은 작동하지만 JSON의 끝에서 "NUL"으로 보이는 것들을 많이 얻고 있습니다. 이유는 모르겠다. 이걸 본 사람 있어요?newtonsoft SerializeXmlNode 후행

코드 :

XmlDocument xmlDoc = BuildTranslationXML(allTrans, applicationName, language); 

// Convert the xml doc to json 
// the conversion inserts \" instead of using a single quote, so we need to replace it 
string charToReplace = "\""; 
string jsonText = JsonConvert.SerializeXmlNode(xmlDoc); 

// json to a stream 
MemoryStream memoryStream = new MemoryStream(); 
TextWriter tw = new StreamWriter(memoryStream); 
tw.Write(jsonText); 
tw.Flush(); 
tw.Close(); 

// output the stream as a file 
string fileName = string.Format("{0}_{1}.json", applicationName, language); 
return File(memoryStream.GetBuffer(), "text/json", fileName); 

파일이 호출하는 웹 페이지까지 제공되며 브라우저는 파일을 저장하라는 메시지를 표시합니다. 파일을 열 때 올바른 JSON을 표시하지만 모든 후행 null도 갖습니다. (잘하면 유래 링크 작품) 아래 이미지 참조 :

file screenshot

답변

2

GetBuffer() 방법은 MemoryStream의 내부 표현을 반환합니다. 대신 Newtonsoft이 들어있는 해당 내부 배열의 일부만 가져 오려면 ToArray()을 사용하십시오.

+0

굉장! 정말 고마워, 효과가 있었다. –

+0

@ToddWilloughby -이 경우 [이 대답 수락] (https://meta.stackexchange.com/q/5234)을 클릭하십시오. – dbc