locked
XML root element missing issue c# RRS feed

  • Question

  • User264732274 posted

    i am just passing xml to UPS web service and web service return some data. some time i am getting error root element missing here is my routine code which throw this error. please guide me how to fix it.

    public ShippingAcceptResult ShippingAccept(string acceptDigestCode)
    {
        requestType = ShipRequestTypes.ShipAccept;
    
        xmlRequest = new System.Xml.XmlDocument();
        xmlRequest.LoadXml(@"<?xml version=""1.0""?>
         <ShipmentAcceptRequest>
           <Request>
              <TransactionReference>
                 <CustomerContext>TR01</CustomerContext>
                 <XpciVersion>1.0001</XpciVersion>
              </TransactionReference>
              <RequestAction>ShipAccept</RequestAction>
              <RequestOption>01</RequestOption>
           </Request>
           <ShipmentDigest>" + acceptDigestCode + "</ShipmentDigest></ShipmentAcceptRequest>");
    
        byte[] bb = new System.Text.ASCIIEncoding().GetBytes(string.Format(XML_CONNECT, sAccessCode.Trim(), sUserId.Trim(), sPassword.Trim()));
    
        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        ms.Write(bb, 0, bb.Length);
        xmlRequest.Save(ms);
        bb = ms.ToArray();
        System.Net.WebClient wc = new System.Net.WebClient();
        xmlRespone = new XmlDocument();
        //System.Web.HttpContext.Current.Response.Write(System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(UPS_URL, "POST", bb)));
        string serverName = (testMode) ? "wwwcie" : "www";
    
        serverName = string.Format(UPS_SERVICE_URL, serverName, ShipRequestTypes.ShipAccept);
        xmlRespone.LoadXml(System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(serverName, "POST", bb)));
        //xmlRespone.Save(@"d:\shippingresponseAccept.xml");
        return new ShippingAcceptResult(xmlRespone);
    }

    this line throwing error xmlRespone.LoadXml(System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(serverName, "POST", bb)));

    the most funniest this if i add a thread.sleep() at the beginning of the routine then error is not happening. please guide me in details where the problem is in code. thanks

    Wednesday, August 12, 2015 7:40 AM

Answers

  • User-84896714 posted

    Hi sudip_inn,

    Thank you for your post.

    sudip_inn

    can u tell me how to validate xml before loading xml by xmldocument class

    You could change your code as follow. Please reference the note on code.

    serverName = string.Format(UPS_SERVICE_URL, serverName, ShipRequestTypes.ShipAccept);
    //try 3 time if server response error
    for (int i = 0; i < 2; i++)
    {
        string responseText = System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(serverName, "POST", bb));
        //check if response text is xml or not
        if (!string.IsNullOrEmpty(responseText) && responseText.Trim().StartsWith("<"))
        {
    
            xmlRespone.LoadXml(responseText);
            return new ShippingAcceptResult(xmlRespone);
        }
    }
    //error after 3 times, return null
    return null;

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 5:38 AM

All replies

  • User753101303 posted

    Hi,

    I would add some additional checks for UploadData. For example I suspect that if the service fails intermitently (they have perhaps something to prevent doing too much queries fro the same IP within a given time frame ?), they may return some HTML markup that warns about the error rather than an XML payload.

    I would check the response content type to see if the returned payload is really XML and/or would trace the what is returned to see how it looks like.

    Wednesday, August 12, 2015 8:30 AM
  • User264732274 posted

    thanks for answer. would u plzz post some code sample to avoid this error.

    Wednesday, August 12, 2015 9:01 AM
  • User264732274 posted

    can u tell me how to validate xml before loading xml by xmldocument class. thanks

    Wednesday, August 12, 2015 9:33 AM
  • User-84896714 posted

    Hi sudip_inn,

    Thank you for your post.

    sudip_inn

    can u tell me how to validate xml before loading xml by xmldocument class

    You could change your code as follow. Please reference the note on code.

    serverName = string.Format(UPS_SERVICE_URL, serverName, ShipRequestTypes.ShipAccept);
    //try 3 time if server response error
    for (int i = 0; i < 2; i++)
    {
        string responseText = System.Text.ASCIIEncoding.ASCII.GetString(wc.UploadData(serverName, "POST", bb));
        //check if response text is xml or not
        if (!string.IsNullOrEmpty(responseText) && responseText.Trim().StartsWith("<"))
        {
    
            xmlRespone.LoadXml(responseText);
            return new ShippingAcceptResult(xmlRespone);
        }
    }
    //error after 3 times, return null
    return null;

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 13, 2015 5:38 AM