none
xml result reading after a http call

    Question

  • Hello,
    I have called a webservice and get as an XML code.
    How can I read this?
    Perhaps even so that it can recognize new fields of the webservice?
    Here is the call:

                using (var handler = new HttpClientHandler { Credentials = new NetworkCredential("user", "password") })
                using (var client = new HttpClient(handler))
                {
                    client.BaseAddress = new Uri(ziel);
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    
                    try
                    {
                        var Result = client.GetAsync(MyCSharpCall).Result;
                        Result.EnsureSuccessStatusCode();
    
                        if (Result.StatusCode == HttpStatusCode.OK)
                        {
                            // Call is ok, read XML here
                            
                        }
                        else
                        {
                            // Call is not ok...
    
                        }
                        
                        
                    }
                    catch
                    {
                        // Call is not OK...
    
                    }
    

    Best Regards

    Bernd

    Thursday, April 20, 2017 11:07 AM

Answers

All replies

  • What does the xml look like?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, April 20, 2017 11:43 AM
    Moderator
  • Sorry, here is the XML Stream:

    <?xml version="1.0" encoding="utf-8" ?> 
    - <entry xml:base="http://sapas1gwe.sapebis.rzspr.local:8000/sap/opu/odata/BSD/PRD_BAG_SERVICE_SRV/" 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">
      <id>http://sapas1gwe.sapebis.rzspr.local:8000/sap/opu/odata/BSD/PRD_BAG_SERVICE_SRV/Bags(BagBatch='160911-201015',Date=datetime'2012-01-07T00%3A00%3A00')</id> 
      <title type="text">Bags(BagBatch='160911-201015',Date=datetime'2012-01-07T00%3A00%3A00')</title> 
      <updated>2017-03-22T12:43:32Z</updated> 
      <category term="PRD_BAG_SERVICE_SRV.Bag" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
      <link href="Bags(BagBatch='160911-201015',Date=datetime'2012-01-07T00%3A00%3A00')" rel="self" title="Bag" /> 
    - <content type="application/xml">
    - <m:properties>
    - <d:BagClassification m:type="PRD_BAG_SERVICE_SRV.BagClassification">
      <d:BagWeight>25.3</d:BagWeight> 
      <d:BagDesignation>Etikett,Leer-, rot (Autom.)</d:BagDesignation> 
      <d:BagType>FNZ</d:BagType> 
      <d:BagStatus>1</d:BagStatus> 
      </d:BagClassification>
      <d:BagBatch>160911-201015</d:BagBatch> 
      <d:Date>2012-01-07T00:00:00</d:Date> 
      </m:properties>
      </content>
      </entry>

    Thursday, April 20, 2017 12:38 PM
  • Use XDocument or XmlDocument. Processing XML in .NET is the same irrelevant of where it came from.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, April 20, 2017 2:03 PM
    Moderator
  • Did you have a sample how it works with my sample (stream from http)...?
    Thursday, April 20, 2017 2:59 PM
  • It doesn't matter where the XML comes from, it is just a string and both types load XML from the string.

    //The HttpResponseMessage
    var msg = client.GetAsync(MyCSharpCall).Result;
    
    //The content as a string
    var xml = msg.Content;
    
    //Loading into XDocument
    var xdoc = XDocument.Parse(xml);
    
    //Loading into XmlDocument
    var doc = new XmlDocument();
    doc.LoadXml(xml);

    Thursday, April 20, 2017 3:14 PM
    Moderator
  • Sorry, but i become a bug...

    I change te code like this:

                        var MyResult = client.GetAsync(MyCSharpCall).Result;
                        MyResult.EnsureSuccessStatusCode();
    
                        if (MyResult.StatusCode == HttpStatusCode.OK)
                        {
                            //The content as a string
                            var xml = MyResult.Content;
    
                            //Loading into XDocument
                            var xdoc = XDocument.Parse(xml);
    
                            //Loading into XmlDocument
                            var doc = new XmlDocument();
                            doc.LoadXml(Convert.ToString(xml));
    
    

    On this line i become an error:

    var xdoc = XDocument.Parse(xml);
    Error 1 The best match for the overloaded 'System.Xml.Linq.XDocument.Parse (string)' method has some invalid arguments. C: \ CSharpUNDSAP \ SAPOData \ SAPOdata Connection \ Program.cs 98 36 SAPOdata Connection

    Error 2 Argument '1': Can not convert 'System.Net.Http.HttpContent' into 'string'. C: \ CSharpUNDSAP \ SAPOData \ SAPOdata Connection \ Program.cs 98 52 SAPOdata Connection

    Thursday, April 20, 2017 7:24 PM
  • Sorry, Content is of type HttpContent so you'll need to get the string content. I'd take a look at ReadAsStringAsync but there might be an easier way.
    Thursday, April 20, 2017 7:38 PM
    Moderator
  • And how?
    Friday, April 21, 2017 7:43 AM
  • Ok, i can read the XML-flie...

    but...

    How can i get the fields:

    BagWeight

    BagDesignation

    BagType

    in the XML file (see on top...)

           foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                            {
                                // search the XML Stream
                                foreach (XmlNode locNode in node)
                                {
                                    // Last Update 
                                    if (locNode.ParentNode.Name == "updated")
                                    {
                                        string lastUpdate = locNode.ParentNode.InnerText;
                                    }
    
                                    // ResultFields:
                                    if (locNode.LocalName == "properties")
                                    {
                                        int anzahl = locNode.ChildNodes.Count;
                                        string StartString = locNode.FirstChild.InnerText;
                                        string EndeString = locNode.LastChild.InnerText;
    
                                        // read BagClassification, BagBatch, Date
                                        XmlNodeList lstActors = locNode.ChildNodes;
                                        for (int j = 0; j < lstActors.Count; j++)
                                            if (j == 0)
                                            {                                         { 
                                              string MyBagBat = lstActors[j].InnerText;
                                            }
                                            else 
                                            if (j == 2)
                                            { 
                                              string MyBagClas = lstActors[j].InnerText;
    
                                            }
                                            else
                                            if (j == 1)
    
                                              string MyBagDate = lstActors[j].InnerText;
                                            }
                           
                                            ;
                                       
                                    }
                                }
    
    
    
    
    
    

    Friday, April 21, 2017 10:29 AM
  • The Problem is that the fields are subfileds...
    Friday, April 21, 2017 10:30 AM
  • I got it ;-)

    • Marked as answer by Bernd Riemke Friday, April 21, 2017 11:30 AM
    Friday, April 21, 2017 11:30 AM