Question

I am trying to read an XML file content embedded in Multimedia Component through templating(C#). The XML file contains few UTF-8 characters. When I read the xml content, the the output is translating the UTF-8 characters into some garbage characters(? symbols or rectangle boxes). Below is the code snippet that I used in C# Templating.

Code 1:

Component xmlMultimediaComponent = (Component)XMLMMSRepositoryObject;
// read xml in multimedia component into a string
UTF8Encoding encoding = new UTF8Encoding();
byte[] binary = xmlMultimediaComponent.BinaryContent.GetByteArray();
string navXmlContent = (binary != null) 
               ? UTF8Encoding.UTF8.GetString(binary, 0, binary.Length) 
                       : string.Empty;           

Code 2:

using (MemoryStream ms = new MemoryStream())
{
  xmlMultimediaComponent.BinaryContent.WriteToStream(ms);
  ms.Seek(0, SeekOrigin.Begin);

  using (var streamReader = new StreamReader(ms, Encoding.UTF8))
  {                      
    string output = streamReader.ReadToEnd();
      ....
  }
}

In both of the above cases, the output is having garbage characters(for UTF-8 encoded).

Any idea how to get the same UTF-8 content into the string output from the XML file in Tridion multimedia component.

Note: The XML File in the multimedia component is saved with UTF-8 encoding.

Thanks in advance.

Was it helpful?

Solution 2

On further investigation, we noticed that the file associated in Multimedia component is ASCII encoded. So there must not be explicit conversion to UTF-8 while reading its contents, and it should go with default encoding(i.e, ASCII in above case).

       Component xmlMultimediaComponent = XMLMMSRepositoryObject as Component;               
       byte[] binary = xmlMultimediaComponent.BinaryContent.GetByteArray();
       string navContent = (binary != null) ? Encoding.GetEncoding("ASCII") : string.Empty;

OTHER TIPS

May I ask why are you trying to load a Xml document into a string?

Try loading your XML Document into an object that understands XML Documents (like XDocument or XmlDocument), since they will know what to do with Byte Order Marks.

Something along the lines of XDocument.Load(stream) (.NET 4).

Then you can use that object's "OuterXml" string property if you really need the text of that document.

EDIT

Reading through your code, it looks like you're basically trying to output XML stored as a binary (or xml that does not comply to a Tridion schema), which is not what I would call a best practice. Anyway, you can set the output of the template as a XmlDocument, doesn't need to be a string. Look at the package.CreateItem variations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top