我正在維護一箇舊的.Net 2.0服務,它從數據庫中讀取數據,並將其序列化爲一個XML文件,以便轉換/輸出爲一個字母。XML編碼間歇性地轉換字符?
它已經運行良好多年,但會間歇性地吐出一個格式錯誤的文件,其中某些字符(>和\ b和\)將被轉換而不是作爲其文字對應文件。
我認爲這可能與編碼有關,但我無法弄清楚它爲什麼會間歇性地做它。請參見下面的一些文件之間的例子(我已經改變了一些名字):
精細:
<?xml version="1.0"?>
<?encoding iso-8859-1?>
<?xml-stylesheet type='text/xsl' href='\\SERVER\FOLDER\bin\stylesheet\blabla.xsl'?>
畸形:
<?xml version="1.0" standalone="yes"?><?encoding iso-8859-1?><?xml-stylesheet type='text/xsl' href='\SERVER\FOLDERin\stylesheet\blabla.xsl'?>
(我沒注意,後者由於某種原因具有standalone =「yes」..這是否會影響它?這兩個文件都是由相同的代碼生成的。)
精細:
<LetterText><FONT size=2 face=Arial>
<P style="MARGIN-RIGHT: 0px" dir=ltr align=left><FONT size=2 face=Arial>Dear Sir/Madam </P>
<P style="MARGIN-RIGHT: 0px" dir=ltr align=left><FONT size=2 face=Arial><STRONG><U>ZERO FARE PASS</U></STRONG></FONT></P>
畸形:
<LetterText><FONT size=2 face=Arial>
<P style="MARGIN-RIGHT: 0px" dir=ltr align=left><FONT size=2 face=Arial>Dear Sir/Madam </P>
<P style="MARGIN-RIGHT: 0px" dir=ltr align=left><FONT size=2 face=Arial><STRONG><U>BLABLABLA BLABLA</U></STRONG></FONT></P>
這裏的,我相信這個交易(再次,我已經改變了一些圍繞着這樣的名稱作爲爲MyObject等)的代碼:
' create a serializer to create the xml output.
mySerializer = New XmlSerializer(GetType(myObject))
' serialize the pass batch to a stream in memory - allows us to edit the stream before outputting to a file
xmlMemoryStream = New MemoryStream()
mySerializer.Serialize(xmlMemoryStream, passes)
' load the xml from the memory stream into an XML Document
xmlMemoryStream.Seek(0, SeekOrigin.Begin)
xmlDocument.Load(xmlMemoryStream)
' set the stylesheet instruction up and add it to the xml document. if
' the stylesheet is set on the print queue then use it
Dim processingInstruction As XmlProcessingInstruction
processingInstructionText = String.Format("type='text/xsl' href='{0}'", oPrintQueueType.stylesheet)
processingInstruction = xmlDocument.CreateProcessingInstruction("xml-stylesheet", processingInstructionText)
xmlDocument.InsertAfter(processingInstruction, xmlDocument.FirstChild)
' we must inform xml parses about special encoding we need to use to display
' unicode charaters in the xml with iso-8859-1 encoding
Dim instruction As XmlProcessingInstruction = xmlDocument.CreateProcessingInstruction("encoding", "iso-8859-1")
xmlDocument.InsertAfter(instruction, xmlDocument.FirstChild)
xmlDocument.PreserveWhitespace = True
' make a new file stream to the desired output file and use it to save the XML Document
' we must write unicode due to the characters in the encoded number string
stream = New System.IO.FileStream(filename, FileMode.Create)
xmlTextWriter = New XmlTextWriter(stream, System.Text.Encoding.Unicode)
xmlDocument.WriteTo(xmlTextWriter)
xmlTextWriter.Flush()
xmlTextWriter.Close()
我想知道是否到ISO-8859-1編碼改爲UTF-8。無論哪種方式,儘管我不理解的地方是爲什麼它間歇地不同,具有相同的代碼,相同的數據,但有時只轉換某些特殊字符。我知道很多方法來排除這些(例如C#中的@符號),但大多數需要訪問原始數據和單個元素。這個應用程序只是拿起並處理它。
任何有這方面經驗的人或者可以給我一個關注點的指針?
這可能是我對XML的經驗不足,但爲什麼要指定編碼是iso-8859-1,然後用'System.Text.Encoding.Unicode'而不是'System.Text創建'XmlTextWriter'。 Encoding.GetEncoding( 「ISO-8859-1」)'? – TnTinMn
不知道說實話。這個系統是多年前由其他人開發的,所以它有很多怪癖(包括這個),我正在努力解決這個問題。我會調整它使用相同的,並試一試,也許這是問題! – Bob
對不起,沒有回到你的隊友,我只是回來工作。你說得對,爲什麼這樣做。我已經將編碼更改爲使用UTF-8,現在似乎可以工作。 (自從問題沒有再發生)。 – Bob