diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/OrtValue.shared.cs b/csharp/src/Microsoft.ML.OnnxRuntime/OrtValue.shared.cs index 86b44a6784817..6b1ff4f7af928 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/OrtValue.shared.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/OrtValue.shared.cs @@ -263,12 +263,16 @@ public ReadOnlyMemory GetStringElementAsMemory(int index) /// UTF-16 string instance public string GetStringElement(int index) { - var chars = GetStringTensorElementChars(index); - if (chars.Length == 0) + GetStringTensorElementBuffer((UIntPtr)index, out uint bytesLen, out IntPtr bufferPtr); + if (bytesLen == 0) { return string.Empty; } - return new string(chars); + + unsafe + { + return new string((sbyte*)bufferPtr.ToPointer(), 0, (int)bytesLen, Encoding.UTF8); + } }