klashagelqvist
klashagelqvist

Reputation: 1261

Open xml encoding

Im using Open XML SDK to read and write information to custom xml parts in a Word document. Im serializing a xml structure into a class , modifying the data and serializing it back into the document.

Example code

DocumentFormat.OpenXml.Packaging.CustomXmlPart myPart = GetCustomXmlPart(mainPart, AITNamespaces.SiteDataNameSpace);
StreamReader myStrR = new System.IO.StreamReader(myPart.GetStream());
string myXML = myStrR.ReadToEnd();
sitedata = ObjectXMLSerializer<WDSiteData>.LoadString(myXML);

Site selSite = _applicationService.GetApplicationData().Sites.Find(item => item.Id == siteId);

sitedata.SiteId= selSite.Id;
sitedata.SiteName = "ööööaaaaaåååå"; // selSite.SiteName;

myXML = ObjectXMLSerializer<WDSiteData>.GetXMLString(sitedata);
myPart = GetCustomXmlPart(wordProcDocument.MainDocumentPart, AITNamespaces.SiteDataNameSpace);
using (StreamWriter sw = new StreamWriter(myPart.GetStream(FileMode.Create)))
{
    sw.Write(myXML);
}

My problem is that the national characters become encoded and the text "ööööaaaaaåååå" is displayed in the word document as "????????aaaaa????????"

The actual encoding is done

myXML = ObjectXMLSerializer<WDSiteData>.GetXMLString(sitedata);

Anyone has a clue on how to go about handling national characters this way.

Upvotes: 1

Views: 3215

Answers (1)

klashagelqvist
klashagelqvist

Reputation: 1261

For whom it concerns , my trouble was that in my serializer the encoding was fixed

return ASCIIEncoding.ASCII.GetString(memStream.ToArray());

When i changed to

return ASCIIEncoding.UTF8.GetString(memStream.ToArray());

all is well.

Upvotes: 2

Related Questions