0

My string xml is as following I have not placed full string as it is too long

Currently my code is as

    public Package GetDeliveryConfirmationLabel(Package package)
    {
        string labeldate = package.ShipDate.ToShortDateString();
        if (package.ShipDate.ToShortDateString() == DateTime.Now.ToShortDateString()) 
            labeldate = "";
           string url= "https://secure.shippingapis.com/ShippingAPI.dll?API=PriorityMailIntlCertify&XML= <PriorityMailIntlCertifyRequest USERID=\"XXXXX\"> <Option></Option> <Revision>2</Revision> <ImageParameters> <ImageParameter>4X6LABEL</ImageParameter> </ImageParameters> <FromFirstName>Garth</FromFirstName> <FromMiddleInitial>A</FromMiddleInitial> <FromLastName>Brooks</FromLastName> <FromFirm>Garths Firm</FromFirm> <FromAddress1>radlab</FromAddress1> <FromAddress2>6406 Ivy Lane</FromAddress2> <FromUrbanization>Garys Urbanization</FromUrbanization> <FromCity>Greenbelt</FromCity> <FromState>MD</FromState> <FromZip5>20770</FromZip5> <FromZip4>1234</FromZip4> <FromPhone>3019187658</FromPhone> <FromCustomsReference> From Customs Ref.</FromCustomsReference> <ToName></ToName> <ToFirstName>Reza</ToFirstName> <ToLastName>Dianat</ToLastName> <ToFirm>HP</ToFirm> <ToAddress1>HP</ToAddress1> <ToAddress2>5th floor</ToAddress2> <ToAddress3>6406 Flower Lane</ToAddress3> <ToCity>Greenbelt</ToCity> <ToProvince>Md</ToProvince> <ToCountry>Canada</ToCountry> <ToPostalCode>20770</ToPostalCode> <ToPOBoxFlag>N</ToPOBoxFlag> <ToPhone>5555555555</ToPhone> <ToFax>3012929999</ToFax> <ToEmail>b@aol.com</ToEmail> <ToCustomsReference>Import Reference</ToCustomsReference> <NonDeliveryOption>Return</NonDeliveryOption> <Container>MDFLATRATEBOX</Container> <ShippingContents> <ItemDetail> <Description>Description 1</Description> <Quantity>1</Quantity> <Value>1.11</Value> <NetPounds>1</NetPounds> <NetOunces>1</NetOunces> <HSTariffNumber>123456789123</HSTariffNumber> <CountryOfOrigin>Brazil</CountryOfOrigin> </ItemDetail> <ItemDetail> <Description>Description 2</Description> <Quantity>2</Quantity> <Value>2.22</Value> <NetPounds></NetPounds> <NetOunces>2</NetOunces> <HSTariffNumber>234567</HSTariffNumber> <CountryOfOrigin>Switzerland</CountryOfOrigin> </ItemDetail> <ItemDetail> <Description>Description 3</Description> <Quantity>3</Quantity> <Value>3.33</Value> <NetPounds></NetPounds> <NetOunces>3</NetOunces> <HSTariffNumber>123456789123</HSTariffNumber> <CountryOfOrigin>Brazil</CountryOfOrigin> </ItemDetail> <ItemDetail> <Description>Description 4</Description> <Quantity>4</Quantity> <Value>4.44</Value> <NetPounds></NetPounds> <NetOunces>4</NetOunces> <HSTariffNumber>234567234567</HSTariffNumber> <CountryOfOrigin>Switzerland</CountryOfOrigin> </ItemDetail> </ShippingContents> <Insured>N</Insured> <InsuredNumber>90123</InsuredNumber> <InsuredAmount>99.90</InsuredAmount> <GrossPounds>3</GrossPounds> <GrossOunces>8</GrossOunces> <ContentType>Documents</ContentType> <ContentTypeOther>and Other</ContentTypeOther> <Agreement>Y</Agreement> <Comments>PriorityMailIntl Comments</Comments> <LicenseNumber>Lic 123</LicenseNumber> <CertificateNumber>Cert456</CertificateNumber> <InvoiceNumber>Inv890</InvoiceNumber> <ImageType>TIF</ImageType> <ImageLayout>TRIMONEPERFILE</ImageLayout> <CustomerRefNo>Cust Ref123</CustomerRefNo> <POZipCode>20770</POZipCode> <LabelDate></LabelDate> <HoldForManifest>N</HoldForManifest> <EELPFC>802.11B</EELPFC> <CommercialPrice></CommercialPrice> <Size></Size> <Length></Length> <Width></Width> <Height></Height> <Girth></Girth> <ExtraServices> <ExtraService></ExtraService> </ExtraServices> </PriorityMailIntlCertifyRequest>"; 

         string xml = web.DownloadString(url);
        if (xml.Contains("<Error>"))
        {
            int idx1 = xml.IndexOf("<Description>") + 13;
            int idx2 = xml.IndexOf("</Description>");
            int l = xml.Length;
            string errDesc = xml.Substring(idx1, idx2 - idx1);
            package.Error = errDesc;
            //throw new USPSManagerException(errDesc);
        }
        else
        {
            int i1 = xml.IndexOf("<LabelImage>") + "<LabelImage>".Length;
            int i2 = xml.IndexOf("</LabelImage>");
            package.ShippingLabel = Convert.FromBase64String(xml.Substring(i1, i2 - i1));

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xml);
            XmlNodeList nodeList = xmldoc.GetElementsByTagName("LabelImage");
            string _DeliveryConfirmationNumber = string.Empty;
            foreach (XmlNode node in nodeList)
            {
                _DeliveryConfirmationNumber = node.InnerText;
            }
            package.ReferenceNumber = _DeliveryConfirmationNumber;
        }
        return package;
    }   

On implementing this code i need to generate the file but its error is looking like

Image error is coming as enter image description here

  • 1
    Just from your wording and function name i would guess you call the wrong function: "I am trying to converting into base 64" vs `FromBase64String` – Roland Starke Aug 31 '17 at 11:23
  • 1
    1. Please show the error message. 2. Did you have a look at the actual substring? – Fildor Aug 31 '17 at 11:24
  • 2
    Your xml string does not contain `PriorityMailIntlCertifyResponse`... – Pikoh Aug 31 '17 at 11:24
  • The issue is not about PriorityMailIntlCertifyResponse the issue is about conversion –  Aug 31 '17 at 11:28
  • 3
    It gets worse. Of course the now changed code must throw this exception, the `xml` string obviously is not a valid base64 string. – René Vogt Aug 31 '17 at 11:28
  • Just a sidenote: "+33" - it's not good to use "magic numbers". Just to reduce the probability of errors you might want to use `string.Length`. – Fildor Aug 31 '17 at 11:30
  • 2
    The issue is trying to decode a base64 string that is not a base64 string. Check your input to get the real string – Pikoh Aug 31 '17 at 11:31
  • here is my actual code and I am getting error at package.ShippingLabel –  Aug 31 '17 at 11:32
  • "error at package.ShippingLabel" - that's been understood. *You* have to check if the string you are trying to decode complies to the specification for base64. ... or if there is a string to begin with. What happens if the document does not contain these tags? Why not convert the whole thing to XmlDocument like you do further down and work on that? – Fildor Aug 31 '17 at 11:35
  • @Fildor actually the string generating has an issue , How can I verify that which string is not proper, what should I need to replace with it. –  Aug 31 '17 at 11:36
  • 1
    That's not how it works. If string *generation* is the problem, then **fix string generation**. "How can I verify that which string is not proper" - you already do! You get an exception. – Fildor Aug 31 '17 at 11:37
  • @Fildor the string is generated dynamically with method web.DownloadString(url); and the DownloadString method is in dll –  Aug 31 '17 at 11:39
  • 1
    So that part is not under your conrol? File a bug report. If it is documented that the resulting xml shall contain a base64-encoded string enclosed by the tag you are using, then this is a bug. – Fildor Aug 31 '17 at 11:41

1 Answers1

1

The problem is that you are not taking the correct part of the returned response (you must not look for the PriorityMailIntlCertifyResponse tag, but LabelImage). Try this:

int i1 = xml.IndexOf("<LabelImage>") + "<LabelImage>".Length;
int i2 = xml.IndexOf("</LabelImage>");
Byte[] vbf = Convert.FromBase64String(xml.Substring(i1,i2-i1));

Edit

As per @Fildor comment, it would be better to use XmlDocument to get the image:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
var imageBase64  = xmldoc.GetElementsByTagName("LabelImage").Item(0).InnerText;
Byte[] image= Convert.FromBase64String(imageBase64);
Pikoh
  • 7,582
  • 28
  • 53
  • 1
    I still would prefer to use the xmlDocument he is using further down in the code. I don't understand why he is working on the string to begin with. It would make much more sense to use XPath to get that value , IMHO. – Fildor Aug 31 '17 at 11:45
  • Me too @Fildor. But as OP is using `Substring` to take the base64 string, i'm using that in my answer :) – Pikoh Aug 31 '17 at 11:46
  • True, just saying ... :) It's just - I don't know the schema for that XML. So xml.IndexOf("") _might_ not get him the correct one ... – Fildor Aug 31 '17 at 11:47
  • 1
    @Fildor edited as it should be :) Yes,it's ``,i've tried with the xml returned from that api – Pikoh Aug 31 '17 at 11:52
  • Pikoh that is fine but can u also help for further code ? further code I mean to say after line XmlDocument xmldoc = new XmlDocument(); –  Aug 31 '17 at 11:52
  • Updated the question –  Aug 31 '17 at 12:02
  • 1
    Sorry @Xtremcool, but that's not a question update, that should be a new question. You can't keeep editing your question to change it. – Pikoh Aug 31 '17 at 12:03
  • u can follow this link where bounty is open. https://stackoverflow.com/questions/45803982/issue-as-the-element-imageparameters-cannot-contain-child-element-imageparame –  Aug 31 '17 at 12:07
  • @Xtremcool, btw, what's in "LabelImage" is a jpg image, not a pdf file. – Pikoh Aug 31 '17 at 12:10