简体   繁体   中英

parsing xml returned by web service in C#

Using this code i got the Response from the webservice.

using (WebResponse response = webRequest.GetResponse())
                {
                    using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                    {
                        string soapResult = rd.ReadToEnd();
                        log.Info(soapResult);
                        return soapResult;

                    }
                }

This code is inside the forloop and the soapResult is concatenated and the final result is like this.

<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header/>
    <soap-env:Body>
        <n0:ExpenseReportRetrivetStatusReadByIDResponse_sync xmlns:n0="http://my.com/xi/myGlobal20/Global" xmlns:prx="urn:my.com:proxy:HPR:/1SIA/TAS90B:804">
            <ExpenseReport>
                <my_UUID>00163e06-cbf3-1ee4-bb86-1330a89d8e78</my_UUID>
                <ExpenseReport_ReimbursementStatusCode>Fully Reimbursed</ExpenseReport_ReimbursementStatusCode>
            </ExpenseReport><Log/>
        </n0:ExpenseReportRetrivetStatusReadByIDResponse_sync></soap-env:Body>
</soap-env:Envelope>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header/>
    <soap-env:Body>
        <n0:ExpenseReportRetrivetStatusReadByIDResponse_sync xmlns:n0="http://my.com/xi/myGlobal20/Global" xmlns:prx="urn:my.com:proxy:HPR:/1SIA/TAS90B:804">
            <ExpenseReport>
                <my_UUID>00163e06-cbf3-1ee4-bb86-16247b9fce78</my_UUID>
                <ExpenseReport_ReimbursementStatusCode>Not Yet Reimbursed</ExpenseReport_ReimbursementStatusCode>
            </ExpenseReport>
            <Log/>
        </n0:ExpenseReportRetrivetStatusReadByIDResponse_sync></soap-env:Body>
</soap-env:Envelope>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/"><soap-env:Header/>
<soap-env:Body>
    <n0:ExpenseReportRetrivetStatusReadByIDResponse_sync xmlns:n0="http://my.com/xi/myGlobal20/Global" xmlns:prx="urn:my.com:proxy:HPR:/1SIA/TAS90B:804">
        <ExpenseReport>
            <my_UUID>00163e06-cbf3-1ee4-bb86-16c473846e78</my_UUID>
            <ExpenseReport_ReimbursementStatusCode>Not Yet Reimbursed</ExpenseReport_ReimbursementStatusCode>
        </ExpenseReport><Log/></n0:ExpenseReportRetrivetStatusReadByIDResponse_sync></soap-env:Body>
</soap-env:Envelope>

I want to retrive the value of ExpenseReport_ReimbursementStatusCode tag.

The final file will be csv file and value will be like

Fully Reimbursed
Not Yet Reimbursed
Not Yet Reimbursed

Can anybody guide me to parse this value in C#.

Please consider i am very new to c#.

Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;

namespace ConsoleApplication20
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test1.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.CSV";
        static void Main(string[] args)
        {
            StreamWriter writer = new StreamWriter(OUTPUT_FILENAME);
            XmlDocument doc = new XmlDocument();
            doc.Load(INPUT_FILENAME);
            XmlNodeList reports =  doc.GetElementsByTagName("ExpenseReport");
            foreach (XmlNode report in reports)
            {
                string my_UUID = report.SelectSingleNode("my_UUID").InnerText;
                string status = report.SelectSingleNode("ExpenseReport_ReimbursementStatusCode").InnerText;
                writer.WriteLine(string.Join(",", new string[] { my_UUID,status}));
            }

            writer.Flush();
            writer.Close();


        }

    }
}

Had to add to your XML a new root. An XML document can't have multiple roots

<?xml version="1.0" encoding="utf-8" ?>
<Envelopes>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Header/>
  <soap-env:Body>
    <n0:ExpenseReportRetrivetStatusReadByIDResponse_sync xmlns:n0="http://my.com/xi/myGlobal20/Global" xmlns:prx="urn:my.com:proxy:HPR:/1SIA/TAS90B:804">
      <ExpenseReport>
        <my_UUID>00163e06-cbf3-1ee4-bb86-1330a89d8e78</my_UUID>
        <ExpenseReport_ReimbursementStatusCode>Fully Reimbursed</ExpenseReport_ReimbursementStatusCode>
      </ExpenseReport>
      <Log/>
    </n0:ExpenseReportRetrivetStatusReadByIDResponse_sync>
  </soap-env:Body>
</soap-env:Envelope>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Header/>
  <soap-env:Body>
    <n0:ExpenseReportRetrivetStatusReadByIDResponse_sync xmlns:n0="http://my.com/xi/myGlobal20/Global" xmlns:prx="urn:my.com:proxy:HPR:/1SIA/TAS90B:804">
      <ExpenseReport>
        <my_UUID>00163e06-cbf3-1ee4-bb86-16247b9fce78</my_UUID>
        <ExpenseReport_ReimbursementStatusCode>Not Yet Reimbursed</ExpenseReport_ReimbursementStatusCode>
      </ExpenseReport>
      <Log/>
    </n0:ExpenseReportRetrivetStatusReadByIDResponse_sync>
  </soap-env:Body>
</soap-env:Envelope>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Header/>
  <soap-env:Body>
    <n0:ExpenseReportRetrivetStatusReadByIDResponse_sync xmlns:n0="http://my.com/xi/myGlobal20/Global" xmlns:prx="urn:my.com:proxy:HPR:/1SIA/TAS90B:804">
      <ExpenseReport>
        <my_UUID>00163e06-cbf3-1ee4-bb86-16c473846e78</my_UUID>
        <ExpenseReport_ReimbursementStatusCode>Not Yet Reimbursed</ExpenseReport_ReimbursementStatusCode>
      </ExpenseReport>
      <Log/>
    </n0:ExpenseReportRetrivetStatusReadByIDResponse_sync>
  </soap-env:Body>
</soap-env:Envelope>
</Envelopes>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM