Re: [問題] LINQ TO XML更改編碼問題

看板C_Sharp作者 (迅雷不及掩耳盜鈴)時間14年前 (2010/05/28 19:59), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
如果你要輸出ascii編碼的"字串"而非"XML", 那要傳出byte[], string永遠是UTF-16的. XDocument doc = XDocument.Parse("<test></test>"); using (MemoryStream stream = new MemoryStream()) { StreamWriter writer = new StreamWriter(stream, Encoding.ASCII); doc.Save(writer); stream.Position = 0; byte[] asciiArray = new byte[stream.Length]; stream.Read(asciiArray, 0, asciiArray.Length); return asciiArray; } ※ 引述《flamo (迅雷不及掩耳盜鈴)》之銘言: : 試試這味 : XDocument doc = XDocument.Parse("<test></test>"); : using (MemoryStream stream = new MemoryStream()) : { : StreamWriter writer = new StreamWriter(stream, Encoding.ASCII); : doc.Save(writer); : stream.Position = 0; : return new StreamReader(stream).ReadToEnd(); : } : ※ 引述《godsing (執著是苦)》之銘言: : : 如題 : : XNamespace xmlns = "http://www.microsoft.com/networking/WLAN/profile/v1"; : : XDocument xdoc = new XDocument( : : new XDeclaration("1.0","US-ASCII",null), : : .......... : : ....... : : ); : : StringWriter sw = new StringWriter(); : : xdoc.Save(sw); : : return sw.ToString(); : : 請問要怎樣改成encoding ASCII?? : : 不管怎樣它都輸出UTF-16.... : : 感覺不難,結果GOOGLE出奇的難找..... : : 感謝解答 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.120.150.76
文章代碼(AID): #1B_x0XrV (C_Sharp)
文章代碼(AID): #1B_x0XrV (C_Sharp)