locked
Cannot parse xml response properly RRS feed

  • Question

  • User-1179262497 posted

    I have the following xml response from an API call:

    <feed xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xml:base="https://sapmobiletest1.mysite.com.au/odata/employee/">
        <id>https://sapmobiletest1.mysite.com.au/odata/employee/Employees</id>
        <title type="text">Employees</title>
        <updated>2016-07-12T14:24:44Z</updated>
        <author>
            <name/>
        </author>
        <link href="Employees" rel="self" title="Employees"/>
        <entry>
            <id>https://sapmobiletest1.mysite.com.au/odata/employee/Employees('90012582')</id>
            <title type="text">Employees('90012582')</title>
            <updated>2016-07-12T14:24:44Z</updated>
            <category term="ZHRGW_EMPLOYEE_SRV.Employee" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
            <link href="Employees('90012582')" rel="edit" title="Employee"/>
            <link href="Employees('90012582')/JobDiscipline" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/JobDiscipline" type="application/atom+xml;type=entry" title="JobDiscipline"/>
            <content type="application/xml">
                <m:properties xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
                    <d:PersonnelNbr>90012582</d:PersonnelNbr>
                    <d:FirstName>Contractor123</d:FirstName>
                    <d:LastName>Test2123</d:LastName>
                    <d:Gender>1</d:Gender>
                    <d:BirthDate>1966-06-06T00:00:00</d:BirthDate>
                    <d:PositionNbr>99999999</d:PositionNbr>
                    <d:JobNbr></d:JobNbr>
                    <d:PersonnelArea>AU04</d:PersonnelArea>
                    <d:PersSubarea>NCHS</d:PersSubarea>
                </m:properties>
            </content>
        </entry>
    </feed>

    What I need to do is to read the entry tag, where there's all these employee data. But whenever I do:

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(response.GetResponseStream());
    
    //Create namespace manager
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
    nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/v1");
    
    foreach (XmlNode row in xmlDoc.DocumentElement.ChildNodes){...

    The ChildNodes only have 5 items: ID, Title, Updated, Author, Link. Where is the Entry tag??? How can read into the entry tag?

    Thanks a lot!

    Tuesday, July 12, 2016 2:31 PM

Answers

  • User-1179262497 posted

    Resolved. Issue due to the api url, need to use & instead of AND

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2016 2:18 AM

All replies

  • User2103319870 posted

    How can read into the entry tag?

    You are trying to read a multilevel xml file.With your code you will get only the first level child nodes. You have a multiple options to read the sublevel nodes like LINQ,XPath etc.

    One easy option it to read the node using its tag name by using GetElementsByTagName Method like below

                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(response.GetResponseStream());
    
                    //Create namespace manager
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                    nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/v1");
                    //Read the intial set of child nodes
                    foreach (XmlNode row in xmlDoc.DocumentElement.ChildNodes)
                    {
    
                    }
                    //Read nodes from entry main nodes
                    XmlNodeList xmlnodes1 = xmlDoc.GetElementsByTagName("entry");
                    foreach (XmlNode row in xmlnodes1)
                    {
    
                    }

    Tuesday, July 12, 2016 9:21 PM
  • User-1179262497 posted
    Thanks. But when I did xmlDoc.GetElementsByTagName("entry"), the list returns 0 count with no result....
    Tuesday, July 12, 2016 11:28 PM
  • User-1179262497 posted

    When I did this:

     using (Stream stream = response.GetResponseStream())
     {
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        String responseString = reader.ReadToEnd();
     }

    it returns:

    "<feed xmlns=\"http://www.w3.org/2005/Atom\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\" xml:base=\"https://sapmobiletest1.mysite.com.au/odata/employee/\">
    <id>https://sapmobiletest1.mysite.com.au/odata/employee/Employees</id>
    <title type=\"text\">Employees</title>
    <updated>2016-07-13T01:11:51Z</updated>
    <author><name/></author>
    <link href=\"Employees\" rel=\"self\" title=\"Employees\"/>
    </feed>"

    Why is that?....I can confirm the API returns the full string though...

    Wednesday, July 13, 2016 1:14 AM
  • User-1179262497 posted

    That's how I make the call:

    WebRequest req = WebRequest.Create(apiUrl);
    req.Method = "GET";
    req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(apiCredential));
    HttpWebResponse response = req.GetResponse() as HttpWebResponse;

    Wednesday, July 13, 2016 1:22 AM
  • User-1179262497 posted

     I am thinking if it's because of the format of the xml response. Apparently if I parse a response which has an XML root tag, then it will get everything, but this one seems to be a bit different with the format, there's not xml root tag, but a feed tag... not sure how to parse it..

    Wednesday, July 13, 2016 1:54 AM
  • User-1179262497 posted

    Resolved. Issue due to the api url, need to use & instead of AND

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 13, 2016 2:18 AM