0

I need to display the processingStatus and every field node value along with it's corresponding name of the following XML.

<?xml version="1.0" encoding="UTF-8"?>
<T24 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.temenos.com/T24/OFSML/130" xsi:schemaLocation="http://www.temenos.com/T24/OFSML/130 ofsml13.xsd">
  <serviceResponse>
    <ofsTransactionProcessed application="FUNDS.TRANSFER" function="INPUT" operation="VALIDATE" processingStatus="OK" version="BOOK.TRAN.VAL.MCB">
      <transactionId>FT15056T2QLP</transactionId>
      <field mv="1" name="TRANSACTION.TYPE" sv="1">AC</field>
      <field mv="1" name="CURRENCY.MKT.DR" sv="1">1</field>
      <field mv="1" name="DEBIT.CURRENCY" sv="1">USD</field>
      <field mv="1" name="DEBIT.AMOUNT" sv="1">125.00</field>
      <field mv="1" name="DEBIT.VALUE.DATE" sv="1">20150225</field>

    </ofsTransactionProcessed>
  </serviceResponse>
</T24>

Can anyone help me please? my code: EDITED CODE:

XmlTextReader reader = new XmlTextReader("XML03.xml");
            Console.WriteLine(reader.Attributes["processingStatus"].Value);
            while (reader.Read())
            {
                Console.WriteLine(reader.Value);

            }
            Console.ReadLine();
5
  • what have you attempted? any search effort? Commented Aug 15, 2016 at 21:02
  • @Steve yes please see edit Commented Aug 15, 2016 at 21:08
  • now please describe whats wrong with the code along with output/exception you are getting Commented Aug 15, 2016 at 21:10
  • I usually use XElement msdn.microsoft.com/en-us/library/… for retrieving values from XML. You may want to look into it for this. If you know the names of the attributes, you should have no problems extracting the values and printing them out. Commented Aug 15, 2016 at 21:12
  • Please see edited code Commented Aug 15, 2016 at 21:30

2 Answers 2

1

One possibility would be to define a type or several types which reflect the structure of the Xml; the members would have to be endowed with the attributes XmlElementAttribute which is documented here, and the attribute XmlAttributeAttribute, which is documented here; if done suitably, the file contents can be deserialized with the XmlDeserializer class, which is documented here. A detailed explanation on how to use the deserialization facilities can be found in this official tutorial.

Sign up to request clarification or add additional context in comments.

Comments

1

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);
            var results = doc.Descendants().Where(x => x.Name.LocalName == "ofsTransactionProcessed").Select(x => new {
                application = (string)x.Attribute("application"),
                function = (string)x.Attribute("function"),
                operation = (string)x.Attribute("operation"),
                processingStatus = (string)x.Attribute("processingStatus"),
                version = (string)x.Attribute("version"),
                transactionId = x.Elements().Where(y => y.Name.LocalName == "transactionId").Select(z => (string)z).FirstOrDefault(),
                fields = x.Elements().Where(y => y.Name.LocalName == "field").Select(z => new {
                    mv = (string)z.Attribute("mv"),
                    name = (string)z.Attribute("name"),
                    sv = (string)z.Attribute("sv"),
                    value = (string)z
                }).ToList()
            }).FirstOrDefault();
        }
    }
}

3 Comments

How big is the xml file? XmlReader isn't needed for small xml files. What tags repeat? I can modify code to work with xml reader but need the elements that repeat. If your input is a string then simply replace Load(FILENAME) with Parse(your xml string).
How to I output all the results? i.e processingStatus and fields?
What format? You can get each property using results.application, results.function, .... You can get the fields using a foreach like : foreach(var field in fields){ field.mn, field.name, field.sv, and field.vaue}

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.