Click here to Skip to main content
15,937,779 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This method is giving me the contents of CDATA section in an xml file. But I want to decode this CDATA.Content of CDATA is the combination of 0s and 1s. But the are encoded so I am getting combination of different alphabets like ABC12KJ etc...
How can I get the decoded data. ?
here is the sample file
<?xml version="1.0" encoding="UTF-8"?>
<TesterLog Version="1">
    <Item name="IUT Name" value="Reference"/>
  <SignalData SamplingPeriod="1000.000 ns" DataWidth="16 bit">

What I have tried:

public void GetCDATA()
            var xdoc = XDocument.Load(xmlfilepath);
            var queryCDATAXML = xdoc.DescendantNodes().OfType<XCData>();

            foreach (var item in queryCDATAXML)

Updated 3-Jun-18 21:34pm
Graeme_Grant 1-Jun-18 18:29pm    
small problem, invalid XML. Change

1 solution

Looks like the data are Base64 - Wikipedia[^] encoded which is common for binary data in XML documents.

Then you have to pass the CDATA strings to a base64 decoder. See the Convert.FromBase64String Method (String) (System)[^].

Untested example (I don't know for sure if item is a String):
foreach (var item in queryCDATAXML)
    byte[] cdata = Convert.FromBase64String(item);
    // Binary data so we have to convert them for display
    Console.WriteLine("   {0}\n", BitConverter.ToString(cdata));
Share this answer
hamid18 1-Jun-18 10:11am    
item is XCData.. so it is not converting item to byte array [] cdata..
Jochen Arndt 1-Jun-18 10:54am    
Have not used that so far but a quick web research assumes that you just have to use the Value property:
byte[] cdata = Convert.FromBase64String(item.Value);
hamid18 1-Jun-18 11:15am    
still i am not getting the required output.. its still the encoded output...
Jochen Arndt 1-Jun-18 11:25am    
What do you mean by "required"?

When item is not of type string, your initial code will not work too.
printing the data as shown in your XML?

Than decoding that is the binary result and printing with the BitConverter should give you a string with hex values.

But that is just for testing. For further processing you have to use the byte array. But I did not know what the data represent and how they have to be used.

If you need for example the single bits, you have to iterate over the array, and for each byte iterate over the 8 bits.
hamid18 4-Jun-18 3:30am    
thanks.. i got it...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900