locked
Specify Nodes in XML Response RRS feed

  • Question

  • User-173651909 posted

    I'm calling a third party API using a webrequest and I successfully receive the response as XML, but I'm not sure how to parse the file (or string in this case).

    The XML returned is as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    <WEResult xmlns="https://www.thuscommercialtools.net" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <SOLUTION_NO>432678</SOLUTION_NO>
    <Tariff>ONE</Tariff>
    <Term>1</Term>
    <Billing>Annually</Billing>
    <Hub_Postcode>N10AA</Hub_Postcode>
    <Hub_Access_Speed>1000</Hub_Access_Speed>
    <Connection>Standard</Connection>
    <Access_Speed>1000</Access_Speed>
    <Node>Exchange Site</Node>
    <Distance>0</Distance>
    <Access_Install>0</Access_Install>
    <Access_Rental>2887</Access_Rental>
    <Standard_Bandwith>100</Standard_Bandwith>
    <Standard_Bandwith_Rental>1667</Standard_Bandwith_Rental>
    <Standard_Burst_Rental>292.5</Standard_Burst_Rental>
    <Enhanced_Bandwith>100</Enhanced_Bandwith>
    <Enhanced_Bandwith_Rental>2194.5</Enhanced_Bandwith_Rental>
    <Enhanced_Burst_Rental>292.5</Enhanced_Burst_Rental>
    <Premium_Bandwith>0</Premium_Bandwith>
    <Premium_Bandwith_Rental>0</Premium_Bandwith_Rental>
    <Total_Rental>7333.5</Total_Rental>
    <CIR>200</CIR>
    <PIR>400</PIR>
    </WEResult>

    This is returned and converted into a string which is loaded into an XElement using XElement.Parse(). I need to extract some of the values returned and assign them to variables for processing.

    string url = "http://blahblahblah";
    string xmlAsString;
    
    WebRequest request = WebRequest.Create(url);
    WebResponse response = request.GetResponse();
    
    using(var reader = new StreamReader(response.GetResponseStream()))
    {
       xmlAsString = reader.ReadToEnd();
    }
    
    XElement xelement = XElement.Parse(xmlAsString);
    IEnumerable<XElement> results = xelement.Elements();
    
                    
    foreach (XElement result in results)
    {
        System.Diagnostics.Debug.WriteLine(result);
        System.Diagnostics.Debug.WriteLine(result.Value);
    }

    The foreach statement outputs the node and the value of the node, but I need to specifically target them. Examples I've seen on the internet explain this clearly for more complex XML (nested and with multiple instances etc) but I can't work out how to assign a particular value to a variable.

    If this were a LINQ statement I'd probably create a new object and work with each one, but I can't figure out LINQ to XML with this file. Something like:

    var results = (from x in xmlfile
                    select new
                    {
                     x.value1,
                     x.value2
                    });

    So my question is how to access the individual values in the returned XML?

    Thanks in advance
    Adam

    Tuesday, January 10, 2017 3:21 PM

Answers

  • User2103319870 posted

    You can read the xml values like below

    //Load you xml here
                XElement doc = XElement.Parse(@"<?xml version='1.0' encoding='UTF-8'?>
                                           <WEResult xmlns='https://www.thuscommercialtools.net' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
                                            <SOLUTION_NO>432678</SOLUTION_NO>
                                            <Tariff>ONE</Tariff>
                                            <Term>1</Term>
                                            <Billing>Annually</Billing>
                                            <Hub_Postcode>N10AA</Hub_Postcode>
                                            <Hub_Access_Speed>1000</Hub_Access_Speed>
                                            <Connection>Standard</Connection>
                                            <Access_Speed>1000</Access_Speed>
                                            <Node>Exchange Site</Node>
                                            <Distance>0</Distance>
                                            <Access_Install>0</Access_Install>
                                            <Access_Rental>2887</Access_Rental>
                                            <Standard_Bandwith>100</Standard_Bandwith>
                                            <Standard_Bandwith_Rental>1667</Standard_Bandwith_Rental>
                                            <Standard_Burst_Rental>292.5</Standard_Burst_Rental>
                                            <Enhanced_Bandwith>100</Enhanced_Bandwith>
                                            <Enhanced_Bandwith_Rental>2194.5</Enhanced_Bandwith_Rental>
                                            <Enhanced_Burst_Rental>292.5</Enhanced_Burst_Rental>
                                            <Premium_Bandwith>0</Premium_Bandwith>
                                            <Premium_Bandwith_Rental>0</Premium_Bandwith_Rental>
                                            <Total_Rental>7333.5</Total_Rental>
                                            <CIR>200</CIR>
                                            <PIR>400</PIR>
                                </WEResult>");
    				XNamespace df = doc.Name.Namespace;
    		//Read solution no
                var SOLUTION_NO = doc.Element(df+"SOLUTION_NO").Value;
                //Read Tariff          
                var Tariff = doc.Element(df+ "Tariff").Value;
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 10, 2017 5:30 PM