-1

I need to convert list of array into xml. In the list the first object is the elements of the xml and from second object onwards it is the values of the elements.

For eg:

list[0]={"firstname","lastname","age","empid"}
list[1]={"john","maxwell","31","101"}
list[2]={"max","lee","45","102"}

Now using above list I need to create an XML file, as mentioned above list[0] needs to used as XML elements and list[1] & list[2] are values for those elements. final XML will look something like this:

<?xml version="1.0" encoding="UTF-8"?>
<EmployeeRecords>
    <Employee>      
        <firstname>john</firstname>
        <lastname>maxwell</lastname>
        <age>31</age>
        <empid>101</empid>
    </Employee>
    <Employee>
        <firstname>Max</firstname>
        <lastname>lee</lastname>
        <dob>45</dob>
        <empid>102</empid>
    </Employee>
</EmployeeRecords>

I have tried using XELement class but I am unable to understand how to pass element names dynamically in it.

XElement xmlout = new XElement("EmployeeRecords", list.Select(i => new XElement("Employee", i.Select(tag=>new XElement("Employee",tag)))));

I have also tried creating elements dynamically using XmlDocument but their also it is not working. Please guide regarding this as I am very new to XML file formats.

2 Answers 2

1

Here is the solution. Note that there is the 2nd parameter for Zip method, that allows you to implement more appropriate names for tuple fields names, instead of First and Second.

The code

using System.Xml.Linq;

string[][] data =
{
    new[] { "firstname", "lastname", "age", "empid" },
    new[] { "john", "maxwell", "31", "101" },
    new[] { "max", "lee", "45", "102" }
};

var xmlout = new XElement("EmployeeRecords",
    data.Skip(1).Select(_ => new XElement("Employee",
        // Zip joins two lists - names at data[0] and values, which are in _
        data[0].Zip(_).Select(_=>new XElement(_.First, _.Second))
    )))
    .ToString();

Console.Write(xmlout);

Output

<EmployeeRecords>
  <Employee>
    <firstname>john</firstname>
    <lastname>maxwell</lastname>
    <age>31</age>
    <empid>101</empid>
  </Employee>
  <Employee>
    <firstname>max</firstname>
    <lastname>lee</lastname>
    <age>45</age>
    <empid>102</empid>
  </Employee>
</EmployeeRecords>
Sign up to request clarification or add additional context in comments.

5 Comments

data[0].Zip(_) is throwing error as it cannot take only 1 argument
What version of .net do you use?
try this: data[0].Zip(_, (name, val) => (name, val)).Select(_=>new XElement(_.name, _.val))
my .net version is 4.6.2
thank you so much for this answer, it is working perfectly. For me the below one worked after updating language version to 7.1. I have marked both as right answer as someone else might find the upper one correct.
0

What you are looking for is called XML Serialization. There is a good introduction to it available at: https://learn.microsoft.com/en-us/dotnet/standard/serialization/introducing-xml-serialization

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.