Unable to access XML data in web service response - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Unable to access XML data in web service response

Last post 04-17-2005 09:21 AM by Agni Jonnalagadda. 10 replies.
Page 1 of 1 (11 items)
Sort Posts: Previous Next
  • 04-15-2005 03:34 AM

    Unable to access XML data in web service response

    I am trying to query a database via a web service using InfoPath SP1. I have a form with employee ID, first name and last name fields.

    In the database, I have a table with EmployeeID, FirstName and LastName. What I want to do is query the FirstName and LastName based on an onAfterChange() event to the EmployeeID field, where the user enters an ID number. The retrieved FirstName and LastName corresponding to the user input EmployeeID must be populated into the respective fields.

    So I have a web service (set up as a Secondary Data Connection) that accepts a String (EmployeeID) and returns an XmlNode containing the last name and first name of the employee.

    Now, as far as calling this web service from InfoPath's JScript, I am running into a JScript-like problem. Here is my code for the onAfterChange() event.

    function msoxd_my_EmployeeID::OnAfterChange(eventObj)
    {
    // Write code here to restore the global state.
    if (eventObj.IsUndoRedo)
    {
    // An undo or redo operation has occurred and the DOM is read-only.
    return;
    }

    // A field change has occurred and the DOM is writable. Write code here to respond to the changes.
    var employeeID = XDocument.DOM.selectSingleNode("//my:EmployeeID").text;

    var employeeInfoWS = XDocument.GetDOM("GetEmployeeInfo");
    employeeInfoWS.setProperty("SelectionNamespaces", "xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" xmlns:s0=\"http://tempuri.org/\"");
    //Set Web Service parameters
    employeeInfoWS.selectSingleNode("/dfs:myFields/dfs:queryFields/s0:GetEmployeeInfo/s0:employeeID").text = employeeID;
    XDocument.DataObjects("GetEmployeeInfo").Query();

    XDocument.UI.Alert(employeeInfoWS.selectSingleNode("/s0:GetEmployeeInfoResponse/s0:lastName").text);

    // Update the main data source with the queried value
    XDocument.DOM.selectSingleNode("//my:LastName").text = employeeInfoWS.selectSingleNode("//s0:lastName").text;
    XDocument.DOM.selectSingleNode("//my:FirstName").text = employeeInfoWS.selectSingleNode("//s0:firstName").text;
    }

    And I get the following error when I try to preview my form:

    Object required
    File:script.js
    Line:126
    XDocument.UI.Alert(employeeInfoWS.selectSingleNode("/s0:GetEmployeeInfoResponse/s0:lastName").text);

    A expected response (sample) from the web service is:

    <?xml version="1.0" encoding="utf-8" ?>
    <empDS>
    <Employees>
    <lastName>Aldrin</lastName>
    <firstName>Edward</firstName>
    </Employees>
    </empDS>

    How do I access the values within the <lastName> and <firstName> tags of the XML response from the web service, from the form's JScript? Thanks!
  • 04-15-2005 09:09 AM In reply to

    Re: Unable to access XML data in web service response

    As per your error message, statement that is causing the problem is
    "XDocument.UI.Alert(employeeInfoWS.selectSingleNode("/s0:GetEmployeeInfoResponse/s0:lastName").text);"

    I think xpath in the above statement should be
    "/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:empDS/s0:Employees/s0:lastName"
    or it can be "//s0:GetEmployeeInfoResponse/s0:empDS/s0:Employees/s0:lastName"
    instead of "/s0:GetEmployeeInfoResponse/s0:lastName"

    Hope this helps

    One tip for you is its always a good idea to use full xpath when you want to select a node in the XML.

    Agni
    InfopathDev India
  • 04-15-2005 11:08 AM In reply to

    Re: Unable to access XML data in web service response

    As Agni said, your XPath is the problem. You have specified a single slash at the beginning... meaning it will start searching for the first node in the XPath from the current location. Since s0:GetEmployeeInfoResponse is NOT the first node, your select methode fails. Had you used a double slash ("//"), it would likely have succeeded.

    But, also, as Agni pointed out, it is best to always use the full XPath where possible. It takes more time to write it out, but in the long run, it is best for several reasons: 1) It performs faster, 2) It leaves no room for doubt as to what you meant, and 3) hmm.. I don't recall what else right now, but I'm sure there's others.

    Good luck.

    Greg Collins [InfoPath MVP]
    InfoPathDev
  • 04-15-2005 01:01 PM In reply to

    Re: Unable to access XML data in web service response

    Thank you very much for the responses. I corrected the above function to include the full (X)path, and I am still unable to retrieve the lastName value. See the code below:

    var lastNameNode = employeeInfoWS.selectSingleNode("/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:GetEmployeeInfoResult/s0:empDS/s0:Employees/s0:lastName");

    XDocument.UI.Alert(lastNameNode.text);

    Interestingly, when I updated the above to:

    var lastNameNode = employeeInfoWS.selectSingleNode("/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:GetEmployeeInfoResult");

    XDocument.UI.Alert(lastNameNode.text);

    ...I got the response (within an alert box) as the following:

    AlrinEdward

    So what is wrong with the original XPath, and how come I get the above when I shorten the XPath specification? FYI, the request specification for the web service method is as follows:

    POST /CMH_InfoPath/Service1.asmx HTTP/1.1
    Host: localhost
    Content-Type: text/xml; charset=utf-8
    Content-Length: length
    SOAPAction: "http://tempuri.org/GetEmployeeInfo"

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <GetEmployeeInfo xmlns="http://tempuri.org/">
    <employeeID>string</employeeID>
    </GetEmployeeInfo>
    </soap:Body>
    </soap:Envelope>

    ...and the response specification is:

    HTTP/1.1 200 OK
    Content-Type: text/xml; charset=utf-8
    Content-Length: length

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
    <GetEmployeeInfoResponse xmlns="http://tempuri.org/">
    <GetEmployeeInfoResult>xml</GetEmployeeInfoResult>
    </GetEmployeeInfoResponse>
    </soap:Body>
    </soap:Envelope>

    The final piece is the WebMethod (web service) code itself, which is as follows:

    [WebMethod]
    public XmlNode GetEmployeeInfo(String employeeID)
    {
    XmlDataDocument xdd = null;

    try
    {
    SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnStr"]);
    DataSet empDS = new DataSet("empDS");
    SqlDataAdapter empDA = new SqlDataAdapter();
    empDA.SelectCommand = new SqlCommand("SELECT lastName, firstName FROM Employees WHERE employeeID = '" + employeeID + "'", conn);
    empDA.Fill(empDS, "Employees");
    xdd = new XmlDataDocument(empDS);
    }
    catch (Exception ex)
    {
    }

    return xdd.DocumentElement;
    }

    This is pretty much all the information on my project. I think my mistake may be silly and small, but my understanding may be quite a bit off as to how all this works!
  • 04-15-2005 09:50 PM In reply to

    Re: Unable to access XML data in web service response

    Hi Aswin,
    My best suggestion for you is, debug your WebMethod (web service) method and look at "xdd.DocumentElement" value
    in quick watch. This way you will know the what is the xml that you are getting into the Infopath.
    With this you can write the Xpath from "root element" to the "leaf element" you want.

    According to my investigation your xpath may be
    "/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:GetEmployeeInfoResult/s0:NewDataSet/s0:Table/s0:lastName";

    If this does not help you please copy paste the xml that you get from the Webservice method.


    Agni
    InfopathDev India
  • 04-16-2005 06:17 AM In reply to

    Re: Unable to access XML data in web service response

    Agni,

    I am not longer using the partial XPath. As I specified in my post(just prior to yours), I am specifying the XPath only upto the point "s0:GetEmployeeInfoResult", and that atleast seems to get me an object, whose text I am able to print. If I go beyond "GetEmployeeInfoResult", into the web service returned XML which is as follows:

    <?xml version="1.0" encoding="utf-8" ?>
    <empDS>
    <Employees>
    <lastName>Aldrin</lastName>
    <firstName>Edward</firstName>
    </Employees>
    </empDS>

    (which I obtained by calling this web service method direcly from the browser), it is not working. Notice however that when I obtain the node representing the above XML and obtain the "text" attribute which is "AlrinEdward". This is equivalent to obtaining the InnerText attribute of a XML node. But if I tried to traverse down to the <lastName> or <firstName> node, it is not able to retrieve the node. Somehow I feel this may be a namespace issue, but I could be wrong. I have included all other details in my first and second posts.
  • 04-16-2005 07:53 AM In reply to

    Re: Unable to access XML data in web service response

    Can you try this please
    var getEmployeeInfoResultNode = employeeInfoWS.selectSingleNode("/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:GetEmployeeInfoResult");
    XDocument.UI.Alert(getEmployeeInfoResultNode.Xml);

    Look at the xml you are getting here and Look for the XPATH in that xml and give it in the following statement.

    var lastNameNode = getEmployeeInfoResultNode.selectSingleNode(XPATH);

    If the above statement does not work try this
    var lastNameNode = getEmployeeInfoResultNode.selectSingleNode("//lastName");

    After this you give full xpath and try it out again. Partial Xpath is wrong I gave it just for testing purpose.

    One more thing i want to say is everything is case sensitive in XML(XPATH).




    Agni
    InfopathDev India
  • 04-16-2005 09:20 PM In reply to

    Re: Unable to access XML data in web service response

    Agni, according to your suggestion, the line:

    var getEmployeeInfoResultNode = employeeInfoWS.selectSingleNode("/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:GetEmployeeInfoResult");
    XDocument.UI.Alert(getEmployeeInfoResultNode.Xml);

    yields an empty popup box. If I replace the Alert code line to:

    XDocument.UI.Alert("Xml: " + getEmployeeInfoResultNode.Xml);

    I get the following in a popup box with the contents-

    Xml: undefined

    So it is strange that this yields nothing, wheras the line-

    XDocument.UI.Alert(getEmployeeInfoResultNode.Text);

    yields "AlrinEdward" in the popup box. Hence, I am unable to proceed with your remaining suggestions.
  • 04-16-2005 11:32 PM In reply to

    Re: Unable to access XML data in web service response

    Sorry man the statement should be
    XDocument.UI.Alert("Xml: " + getEmployeeInfoResultNode.xml);

    Just now I realised that "xml" should be in lowercase.

    Hope this helps.


    Agni
    InfopathDev India
  • 04-17-2005 05:14 AM In reply to

    Re: Unable to access XML data in web service response

    Agni, I was able to output with "xml" instead of "Xml", and here is what shows up in the Alert box -

    <GetEmployeeInfoResult xmlns="http://tempuri.org/"><empDS xmlns=""><Employees><lastName>Aldrin</lastName><firstName>Edward</firstName></Employees></empDS></GetEmployeeInfoResult>

    I notice that the returned XML (from the web service method) does not have any namespace, so I experimented with the XPath:

    XDocument.UI.Alert(employeeInfoWS.selectSingleNode("/dfs:myFields/dfs:dataFields/s0:GetEmployeeInfoResponse/s0:GetEmployeeInfoResult/empDS/Employees/lastName").text);

    and I was able to obtain the last name! But I would still like to know how this XML ended up with no namespace, and how (and where) do I set it?

    Thank you very much for your help!
  • 04-17-2005 09:21 AM In reply to

    Re: Unable to access XML data in web service response

    I am not sure, but this is what my understanding is
    XML that gets generated by the webservice at compile time will have prefixes, and XML that gets generated at runtime will not have any prefixes.

    For example lets take your webservice itself
    "<s0:EmployeeInfoResponse>" and "<s0:GetEmployeeInfoResult>" both have prefixes because these both are known to the webservice at compile time(This will be there in the webservice 'SOAP')
    where as the nodes <empDS>, <Employees> and <lastName> dont have prefixes because this data that got generated in run time.

    Probably this is half knowledge, please post it in the site if you get better information about this topic.

    Agni
    InfopathDev India
Page 1 of 1 (11 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.