-1

I have an XML that implements from IXmlSerializable. I want to add a CData property so I can add binary data to the property in the XML. I'm going to pass the XML along and in another application use the binary data.

I know some characters won't be able to translate certain characters but ignoring that fact, how would I achieve this? I tried several ways from stackoverflow but I have been unsuccessful.

    [XmlElementAttribute(ElementName = "test", Form = XmlSchemaForm.Unqualified)]
    [XmlElement("CDataElement")]
    public RawXml test
    {
        get
        {
            return test;
        }
        set
        {
            test= value;
        }
    }                                   

byte[] bAry= BinaryData;
item.Property= new CustomXML(bAry);

"item" and "CustomXML" both derive from IXmlSerializable.

user9864738
  • 17
  • 1
  • 7
  • I would convert to 64 base string : string = Convert.ToBase64String(byte[]) and byte[] = Convert.FromBase64String(string) – jdweng May 29 '18 at 14:02

2 Answers2

1

XML is a text based container. You cannot place binary data within a text based container without transforming it to some kind of character based translation.

Most engines will take the binary and encode it as base64 implicitly.

C# XmlWriter has the method XmlWriter.WriteBase64.

Of course you can use Convert.ToBase64String() in order to translate this yourself and pass it in as any other string value.

It should not be necessary to think about this at all...

And just to mention: No need for a CDATA section here. CDATA is something you do not need at all and which should be avoided...

Shnugo
  • 66,100
  • 9
  • 53
  • 114
  • Just want to make sure. If I have a byte array that has "\n"(new line) or the converted base64 that had a new line and I am sending it in "line mode", where it ends if it detects end of line, how would I go around that issue? According to wiki base64 doesn't have a backslash so I don't have to worry about new lines? – user9864738 May 29 '18 at 21:08
  • @user9864738 If you see the data as *binary data*, there is no *new line* or any other meaningful content... Just a bit pattern. Any content can be translated to `base64`, which is nothing more than a chain of characters taken from a reduced set. But maybe I did not get your question... – Shnugo May 29 '18 at 21:30
  • application that receives it ends when it detects a new line. Is it possible that the translated base64 will contain a newline from the data? I don't see a "\" sign for base64 so I don't have to worry about some characters being translated to "\n" which would terminate my msg earlier – user9864738 May 30 '18 at 13:53
  • @user9864738 one of the base principles of base64 is the reduced character set. So, no problem here – Shnugo May 30 '18 at 14:00
0

You'll have to do:

Convert.ToBase64String(yourBinaryData) and pass it as a string in CData, then at the other end you'll want to use: Convert.FromBase64String(yourCDataText)

If you really wanted to send binary data over XML you could do:

<DATA>
  <BINARY>
    <BIT index="0">0</BIT>
    <BIT index="1">0</BIT>       
    ...
    <BIT index="99">1</BIT>
  </BINARY>
</DATA>

However you're better off just doing base64 encoding.

Ryan McDonough
  • 9,732
  • 3
  • 55
  • 76
  • When I got this task, the senior developer told me there's a way I can send it in "binary mode" so I don't have to change it to base64. So you're telling there's absolutely no way I can insert the binary byte array into CData? – user9864738 May 29 '18 at 14:08
  • @user9864738 As written in my answer: XML is a text based container. Binary data must be changed to a text based translation. Most common is `base64`, but there are other options too (HEX, base32, base85...) – Shnugo May 29 '18 at 14:16
  • @user9864738 The only reason `CDATA` might help, is in case of **forbidden characters**. This happens, if you string contains `<, > or &` or unusual code points. But it is the better approach to escape these characters (`<` -> `<`) than to use a `CDATA` section. Semantically there is no difference. – Shnugo May 29 '18 at 14:20
  • @user9864738 - Perhaps the senior developer had in mind the transfer of data as multipart MIME type. See [1](http://www.xml.com/pub/a/98/07/binary/binary.html), [2](https://msdn.microsoft.com/en-us/library/ms996427.aspx), [3](https://www.w3.org/TR/xml-media-types/). Or maybe he meant one of the [binary xml](https://en.wikipedia.org/wiki/Binary_XML) formats. – Alexander Petrov May 29 '18 at 14:48
  • @Shnugo We don't want the XML to deserialize the binary data. We just want to embed the binary data and make the XML ignore it completely. Another application will come and deserialize binary data. You're saying that's still impossible since you can't put byte array in CData? Thank you for you response. – user9864738 May 30 '18 at 13:56