1

I am trying to extract the following values from this XML using C#.

Messages - the inntertext
success="true" - just the "true" bit
status="Released" - just the "Released" bit

Namespaces are clearly the issue here - but I can't get it right. Can someone provide an example of parsing this file here please?


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
    <tns:executeCreateLoanIncreaseResponse xmlns:tns="http://api.ws.liq.pisys.com/">
      <CreateLoanIncreaseResponse success="true">
        <OutstandingTransactionAsReturnValue version="1.0" alias="LIBRLC" status="Released" id="5CJB6GS"></OutstandingTransactionAsReturnValue>
        <Messages>
          <Message>CreateLoanIncrease&gt;&gt;Effective date current business date (08-Sep-2016).</Message>
          <Message>CreateLoanIncrease&gt;&gt;Intent Notices have not been generated.</Message>
          <Message>CreateLoanIncrease&gt;&gt;You are about to release this increase.</Message>
        </Messages>
      </CreateLoanIncreaseResponse>
    </tns:executeCreateLoanIncreaseResponse>
  </soapenv:Body>
</soapenv:Envelope>

2 Answers 2

2

Try xml linq :

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            XElement messages = doc.Descendants().Where(x => x.Name.LocalName == "Messages").FirstOrDefault();
            XNamespace ns = messages.GetDefaultNamespace();

            List<string> strMessages = messages.Elements(ns + "Message").Select(x => (string)x).ToList();
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the rapid response! This worked perfectly.
1

You can use Linq2Xml and XPath

var xDoc = XDocument.Load(filename);

var success = (bool)xDoc.XPathSelectElement("//CreateLoanIncreaseResponse").Attribute("success");
var status = (string)xDoc.XPathSelectElement("//OutstandingTransactionAsReturnValue").Attribute("status");
var messages = xDoc.XPathSelectElements("//Message").Select(x => (string)x.Value).ToList();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.