Jump to content

extracting cdata from xml and decoding to binary

Hi,

 

I'm pretty new to the programming world and I've been given an assignment to extract cdata from xml and then decoding it to binary by using base64 using Visual Studio. For the love of god I've been searching for 5 days on how to do this and I feel like throwing myself off a cliff. Can some kind souls help me with this and explain along the way? Unfortunately, I can't give examples as it is against my companies policy. But the data goes:

 

<?xml version="1.0" encoding="UTF-8"?> 
<DispatchData>
<TransactionId>526</TransactionId>
<ContentFormat>CSV</ContentFormat>
<Compressed>Y</Compressed>
<Data><![CDATA[insert data here]]></Data>
</DispatchData>

 

Thanks for taking the time to read this. T.T

Link to comment
Share on other sites

Link to post
Share on other sites

Does it specify which language to use?

CPU:

Intel Core i5 2500k - Motherboard: Asus maximus iv gene-z - RAM: 2x Corsair Vengeance Blue 4GB DDR3-1600 CL9 - GPU: ASUS GTX 770 DirectCU II
Case: Coolermaster Centurion 5 II - Storage: Crucial M4 128GB, Seagate barracuda 3TB PSU: XFX 650W XXX Edition Modular PSU - Keyboard: Ducky Shine 2 Pro
Mouse: Razer Deathadder 2013 - Sound: Razer Characias
Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Alterlai said:

Does it specify which language to use?

Yeah. I'm supposed to use C#

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, therealshamfake said:

Yeah. I'm supposed to use C#

What part of the assignment are you stuck on exactly? I'm no C# expert but I might be able to help you find some documentation.

CPU:

Intel Core i5 2500k - Motherboard: Asus maximus iv gene-z - RAM: 2x Corsair Vengeance Blue 4GB DDR3-1600 CL9 - GPU: ASUS GTX 770 DirectCU II
Case: Coolermaster Centurion 5 II - Storage: Crucial M4 128GB, Seagate barracuda 3TB PSU: XFX 650W XXX Edition Modular PSU - Keyboard: Ducky Shine 2 Pro
Mouse: Razer Deathadder 2013 - Sound: Razer Characias
Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Alterlai said:

What part of the assignment are you stuck on exactly? I'm no C# expert but I might be able to help you find some documentation.

I'm still stuck in the extraction process. I am really new to C# as well and like I am so lost right now I don't know where to start

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, therealshamfake said:

I'm still stuck in the extraction process. I am really new to C# as well and like I am so lost right now I don't know where to start

Here's one way of accomplishing this:

using ConsoleApp1.Properties;
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = XDocument.Parse(Resources.SomeXml);
            var data = document.DescendantNodes().Where(n => n.NodeType == XmlNodeType.CDATA).Select(n => n.Parent.Value.Trim());

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

Given the file:

<?xml version="1.0" encoding="UTF-8"?>
<DispatchData>
	<TransactionId>526</TransactionId>
	<ContentFormat>CSV</ContentFormat>
	<Compressed>Y</Compressed>
	<Data><![CDATA[Some encapsulated data]]></Data>
</DispatchData>

One can use LINQ to XML to be more specific about the node selection.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Nuluvius said:

Here's one way of accomplishing this:


using ConsoleApp1.Properties;
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var document = XDocument.Parse(Resources.SomeXml);
            var data = document.DescendantNodes().Where(n => n.NodeType == XmlNodeType.CDATA).Select(n => n.Parent.Value.Trim());

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

Given the file:


<?xml version="1.0" encoding="UTF-8"?>
<DispatchData>
	<TransactionId>526</TransactionId>
	<ContentFormat>CSV</ContentFormat>
	<Compressed>Y</Compressed>
	<Data><![CDATA[Some encapsulated data]]></Data>
</DispatchData>

One can use LINQ to XML to be more specific about the node selection.

Thank you very much Nuluvius. I will try it out when my head's not spinning. Will update you guys again soon XOXO

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×