Click here to Skip to main content
15,880,967 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. ?
Thanks..
here is the sample file
<?xml version="1.0" encoding="UTF-8"?>
<TesterLog Version="1">
  <TestProperties>
    <Item name="IUT Name" value="Reference"/>
  </TestProperties>
  <SignalData SamplingPeriod="1000.000 ns" DataWidth="16 bit">
    <Signal>
      <Id>IUT_RX</Id>
      <InitState>1</InitState>
      <![CDATA[HQFPAVkBiwGVAZ8BqQHHAdEBAwINAjUCPwJxAnsCrQK3AsEC1QLzAv0CEQMbAzkDTQNrA3UDfwOJA7sDxQPtA/cDKQQzBEcEUQSDBI0EtQTJBN0E5wTxBAUFDwUZBS0FNwVBBUsFVQWHFZEVmxWlFa8VuRXDFc0V1xXhFesV9RX/FTEWOxZFFk8WgRaLFpUWnxapFscW0RbbFuUW7xYDFyEXPxdJF1MXGhgkGC4YTBhWGHQYfhiwGLoY2BjiGBQZHhkoGTIZUBlaGXgZghmgGaoZvhnbGeUZ9RwTHR0dTx1ZHYsdlR29Hccd+R0DHg0eFx5JHlMeZx6ZHsEe6R4lH5Qfsh+8H+4f+B8qIDQgXCBmIJggoiCsILYg6CDyIAYhOCFgIYghxCEzIlEiWyKNIpciySLTIvsiBSM3I0EjSyNVI4cjmyOlI9cj/yMTJB0kpyaxJrsmxSbPJgEnCyc9J0cnZSdvJ6EnqyfdJ+cnGSgjKC0oQShLKF8ocyiHKJsopSivKLkowyjWKOAo8jQkNS41YDVqNZw1pjXENc41ADYKNhQ2HjZGNlA2WjZkNm42eDaWNqo2tDbHNtE2uDd=]]>
    </Signal>
    <Signal>
      <Id>CCT_TX</Id>
      <InitState>1</InitState>
      <![CDATA[HQFPAVkBiwGVAZ8BqQHHAdEBAwINAjUCPwJxAnsCrQK3AsEC1QLzAv0CEQMbAzkDTQNrA3UDfwOJA7sDxQPtA/cDKQQzBEcEUQSDBI0EtQTJBN0E5wTxBAUFDwUZBS0FNwVBBUkXUxfbGeUZ1ijgKMc20Ta4N0==]]>
    </Signal>
//////////////////////////////////////////////////////////////////////////////////////////////////////
  </SignalData>
</TesterLog>


What I have tried:

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

            foreach (var item in queryCDATAXML)
            {
                Console.WriteLine(item);
                Console.WriteLine();
            }

        }
Posted
Updated 3-Jun-18 21:34pm
v2
Comments
Graeme_Grant 1-Jun-18 18:29pm    
small problem, invalid XML. Change
</CanConformanceTesterLog>
to
</TesterLog>

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));
    Console.WriteLine();
}
 
Share this answer
 
Comments
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.
Is
WriteLine(item.Value);
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