Trevor
Trevor

Reputation: 11

How can I list XML elements in uniform columns after using concat?

I'm converting XML to CSV and I want the converted records to show in uniform columns under particular headers

I've tried using the following code to concat XML elements in strings:

string[] valuesToPrint = { "oper_type", "msg_type", "oper_date","host_date","amount_value", "currency"};
        //string[] newValues = {};

        XNamespace ns = "http://in/SVXP/Settlement";

        File.WriteAllLines(@"C:\Users\test.csv",
        new[] { string.Join(",", valuesToPrint) }

        .Concat(XDocument.Load(filename)
        .Descendants(ns + "operation")
        .Select(e =>
        {
            return string.Join(",", e.Attributes()
            .Where(a => valuesToPrint.Contains(a.Name.LocalName))
            .Select(a => a.Value.Replace(",", ""))
            .Concat(e.Elements()
            .Where(c => valuesToPrint.Contains(c.Name.LocalName))
            .Select(c => c.Value.Replace(",", ""))).ToArray());
        }))
        .Concat(XDocument.Load(filename)
        .Descendants(ns + "oper_amount")
        .Select(e =>
        {
            return string.Join(",", e.Attributes()
            .Where(a => valuesToPrint.Contains(a.Name.LocalName))
            .Select(a => a.Value)
            .Concat(e.Elements()
            .Where(c => valuesToPrint.Contains(c.Name.LocalName))
            .Select(c => c.Value)).ToArray());
        }))
        );

I expect the output to be like:

OPTP0001    MSGTAUTH    21/07/2019  21/07/2019  12250   598

However, it is appearing as:

OPTP0001    MSGTAUTH    21/07/2019  21/07/2019 ............................................12250    598

Upvotes: 0

Views: 33

Answers (0)

Related Questions