开发者

Saving unicode characters as "“" into a file; C#

开发者 https://www.devze.com 2023-02-09 20:12 出处:网络
I have an xml file (converted from xfdl) which contains something like: <custom:popUp xfdl:compute=\"toggle(activated,\'off\',\'on\') == \'1\' ? viewer.messageBox(\'o Once you click ..... page.&am

I have an xml file (converted from xfdl) which contains something like:

<custom:popUp xfdl:compute="toggle(activated,'off','on') == '1' ? viewer.messageBox('o Once you click ..... page.&#xD;o When you use the &#x201C;Create &#x201D; function in.......Portal.','Information'):''">

I load it and save it using...

XmlDocument xmlOut = new XmlDocument(); //note: not read only
FileStrea开发者_JS百科m outfs = new FileStream(tempOutXmlFileName, FileMode.Open, FileAccess.Read,
                           FileShare.ReadWrite);
xmlOut.Load(outfs);
xmlOut.Save(tempOutXmlFileName);
outfs.Close();

This process converts some of the unicode instructions into actual characters which completely messes up the xml/xfdl parsing as there are now quotation marks where quotation marks shouldn't be.

Does anybody know a way I can save the file with all the lovely &#x201C; characters intact?

Thank you.


Well, after fiddling around for a bit and getting the xml->xfdl conversion working better, I ran into a new problem.

The solution below seems to work and all the parsing of the xml is correct, but the program to read the xfdl file doesn't seem to like when I encode it using UTF-8 and wants the encoding to be ISO-8859-1.

Any ideas?


Using StreamReader and StreamWriter should help. To be clear you are trying to read from and write to the same file? I added some nice using statements aswell.

        XmlDocument xmlOut = new XmlDocument(); 
        //note: not read only
        using (FileStream outfs = new FileStream(tempOutXmlFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (StreamReader reader = new StreamReader(outfs, Encoding.UTF8))
        {
            xmlOut.Load(reader);
        }

        using (StreamWriter writer = new StreamWriter(tempOutXmlFileName, false, Encoding.UTF8))
        {
            xmlOut.Save(writer);
        }

I set append to false in the StreamWriter, seems to make sense.


Turns out that reading and writing the files byte by byte solved the problem since the writer never got the opportunity to do any interpretation on the content.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号