0

I am trying to get the baseCode64 Data within my response data. I am getting the ReturnCode but how can I get the data inside the "Payload".

For example my response data looks like this:

 <xml_response xsi:type="xsd:string"><![CDATA[<CertificateRequest><ReturnCode>0</ReturnCode><Payload content_type="application/pdf" embedded="base64">SGVsbG8gV29ybGQ=</Payload></CertificateRequest>]]></xml_response>

To get the ReturnValue I have coded this:

  XElement xmlTree = XElement.Parse(response_data);
  XElement returnCode = xmlTree.Element("ReturnCode");
  XText returnCode_Value = returnCode.FirstNode as XText;
  String b1 = returnCode_Value.Value;      

Now, how can I get the Value inside the Payload which I to convert in plaintext or create a pdf.

I tried to use the same way with paylaod but i doesn't work. I am getting nothing:

XElement returnCode = xmlTree.Element("Payload");

An if I display the Elements with:

XElement xmlTree = XElement.Parse(response_data);
XElement new_data = xmlTree.Elements();

I am just getting:

0

I has been displayed the Element Payload. This is very interesting but why?

11
  • What is getStringData_b1_de? A short but complete program would be useful here. Commented Mar 16, 2015 at 16:07
  • 1
    What is blocking you? You seem to be able to read the ReturnCode, so i don't see why you wouldn't be able to read the Payload? Commented Mar 16, 2015 at 16:08
  • I need the basaCode64 Value inside the payload tag. I am not getting the data if I call XElement returnCode = xmlTree.Element("Payload"); Commented Mar 16, 2015 at 16:14
  • could you update your attempts inside your starting post (edit) Commented Mar 16, 2015 at 16:15
  • 1
    When you say XElement returnCode = xmlTree.Element("Payload"), what is the value of returnCode after that line? A null? An XElement with no content? Commented Mar 16, 2015 at 16:30

3 Answers 3

1

I would simply read the content of the response using the XmlSerializer, it makes it very easy to read the data into the object, and even the decoding could be done over a hidden property

So to read the certificate request, you could go for the following 2 classes

public class CertificateRequest
{
    [XmlElement("ReturnCode")]
    public int ReturnCode { get; set; }

    [XmlElement("Payload")]
    public Payload Payload { get; set; }
}

public class Payload
{
    [XmlAttribute("content_type")]
    public string ContentType { get; set; }
    [XmlAttribute("embedded")]
    public string Embedded { get; set; }
    [XmlText]
    public string Value { get; set; }

    [XmlIgnore]
    public string DecodedValue
    {
        get
        {
            if (string.IsNullOrWhiteSpace(Value))
            {
                return string.Empty;
            }
            return System.Text.Encoding.ASCII.GetString(Convert.FromBase64String(Value));
        }
    }
}

and then read the string using a memorystream to deserialize it to a certificate request object, as in the following way:

class Program
{
    static CertificateRequest DeserializeRequest(string content)
    {
        CertificateRequest request = null;
        using (MemoryStream ms = new MemoryStream())
        {
            byte[] data = System.Text.Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\"?>" + content);
            ms.Write(data, 0, data.Length);
            ms.Position = 0;
            XmlSerializer xs = new XmlSerializer(typeof(CertificateRequest));
            request = xs.Deserialize(ms) as CertificateRequest;
        }
        return request;
    }

    static void Main(string[] args)
    {
        string xmlAsString = @"<CertificateRequest><ReturnCode>0</ReturnCode><Payload content_type=""application/pdf"" embedded=""base64"">SGVsbG8gV29ybGQ=</Payload></CertificateRequest>";
        CertificateRequest request = DeserializeRequest(xmlAsString);
        Console.WriteLine(request.Payload.Value);
        Console.WriteLine(request.Payload.DecodedValue);
        Console.ReadLine();
    }
}

which would then print the base64 encoded value + Hello world on the second line (good one :D)

Sign up to request clarification or add additional context in comments.

Comments

0

You don't show the code where you're attempting to access the value of Payload, so I'm sort of guessing what you want to do, but try this:

XElement payload = xmlTree.Element("Payload");
string b2 = payload.Value;

1 Comment

Nope its not working :-( I can't access the tag Payload. Just Certificate.. and the ReturnCode tag
0

First off, your XML won't load because it's missing a namespace declaration for xsi. I fixed it by adding the following:

<xml_response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:type="xsd:string" ...

Having done that, your XML contains an embedded text string literal (represented as CDATA) which is itself XML. Therefore you must:

  1. Parse the outer XML.
  2. Extract the character data as a string.
  3. Parse the inner XML.

For instance:

        string response_data = @"<xml_response xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xsi:type=""xsd:string""><![CDATA[<CertificateRequest><ReturnCode>0</ReturnCode><Payload content_type=""application/pdf"" embedded=""base64"">SGVsbG8gV29ybGQ=</Payload></CertificateRequest>]]></xml_response>";

        var xmlTree = XElement.Parse(response_data);
        var innerXml = xmlTree.Value; // Extract the string literal
        var innerXmlTree = XElement.Parse(innerXml); // Parse the string literal as XML
        var payload = innerXmlTree.Element("Payload").Value; // Extract the payload value
        Debug.Assert(payload == "SGVsbG8gV29ybGQ="); // No assert.

6 Comments

Currently it is not possible to change the response data. I should work with the old one. But thanks, I have to search another solution.
@machupichu - does your response data actually contain xsi:type="xsd:string" without a definition for xsi? Or did you simply omit that part of the XML when copying it into your question?
@machupichu - because if your XML lacked the namespace declaration, your line of code XElement xmlTree = XElement.Parse(response_data); would have thrown an exception. Suggest you confirm that your actual XML lacks the xsi namespace declaration.
@machupichu - I don't understand the question in your comment. Solve which - the lack of an xsi declaration, or extracting inner character data and parsing it to XML? The code I provided will correctly extract the payload string as long as the outer XML is well formed.
I meant extracting the data.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.