在C#中解析Web服务返回的xml

时间:2015-04-27 10:49:19

标签: c# xml

使用此代码,我从网络服务获得了响应。

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

                    }
                }

此代码位于forloop内,soapResult连接在一起,最终结果如下所示。

<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>

我想要检索ExpenseReport_ReimbursementStatusCode标记的值。

最终文件将是csv文件,值将类似于

Fully Reimbursed
Not Yet Reimbursed
Not Yet Reimbursed

任何人都可以指导我在C#中解析这个值。

请考虑我是c#的新手。

1 个答案:

答案 0 :(得分:0)

试试这个

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();


        }

    }
}

必须向您的XML添加新根。 XML文档不能有多个根

<?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>