0

In Dynamics CRM online, I have a record in the annotation entity which has an XML attached. From a plugin, I need to get the content of that attachment and create an XML element so I can deserialize its content as an object class and apply some logic.

As we are in an online implementation, I cannot do things like create a file and read its content. So what we have is a document content in the attached file, which is a string with multiple characters, as you can see here (attachment is generated by a Power Automate flow):

enter image description here

Here is some of my code:

            byte[] fileContentBytes = Encoding.Default.GetBytes(note.DocumentBody);
            MemoryStream stream = new MemoryStream(fileContentBytes);

            StreamReader globalReader = new StreamReader(stream);

            XmlDocument doc = new XmlDocument();

            doc.Load(globalReader);

The error message says that Data at the root level is invalid. Line 1, position 1, as it's trying to parse as an XML something that is not parse-able as that.

Manuel Roldan
  • 487
  • 3
  • 12

2 Answers2

1

The document body is base 64 encoded.

Try the following

byte[] fileContentBytes = Convert.FromBase64String(note.DocumentBody);
MemoryStream stream = new MemoryStream(fileContentBytes);

StreamReader globalReader = new StreamReader(stream);

XmlDocument doc = new XmlDocument();

doc.Load(globalReader);
Brent
  • 106
  • 2
0

Power Automate is generating JSON document, you are trying to parse it as XML, so failing.

Instead try some methods to convert to your needed format.

enter image description here