Click here to Skip to main content
15,917,320 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello developers,

I want to convert comma separated string into xml structure using linq
My String variable is:

C#
var Query = "ID=265465,265466,265467,265468" + "|" +
"class=6,6,6,6" + "|" +
"name=ABC,DEF,GHI,JKL";


output : ID=265465,265466,265467,265468|
class=6,6,6,6|
name=ABC,DEF,GHI,JKL

I need this into following xml structure

XML
<Students>
    <stud>
        <ID>265465</ID>
        <class>6</class>
        <name>ABC</name>
    </stud>
    <stud>
        <ID>265466</ID>
        <class>6</class>
        <name>DEF</name>
    </stud>
    <stud>
        <ID>265467</ID>
        <class>6</class>
        <name>GHI</name>
    </stud>
    <stud>
        <ID>265468</ID>
        <class>6</class>
        <name>JKL</name>
    </stud>
</Students>


Currenty I am using this code

SQL
var query = new XElement("Students",
                                    from node in Query.Split('|')
                                       select
                                            new XElement("stud",
                                                from n in node.Split('=')[1].Split(',')
                                                    select
                                                        new XElement(node.Split('=')[0], n.Trim())));


In this code I am getting the following output

XML
<Students>
    <stud>
        <ID>265465</ID>
        <ID>265466</ID>
        <ID>265467</ID>
        <ID>265468</ID>
    </stud>
    <stud>
        <class>6</class>
        <class>6</class>
        <class>6</class>
        <class>6</class>
    </stud>
    <stud>
        <name>ABC</name>
        <name>DEF</name>
        <name>GHI</name>
        <name>JKL</name>
    </stud>
</Students>


I want to do this using linq
not by loop in c#.

Please help me guys to achieve my work...
Posted

1 solution

I am not sure if this is the best way to do it, but it does get the result you are looking for using linq with xml:
C#
var query = new XElement("Students",
	from node0 in Query.Split('|')[0].Split('=')[1].Split(',').Select((item, index) => new { item, index })
	join node1 in Query.Split('|')[1].Split('=')[1].Split(',').Select((item, index) => new { item, index })
		on node0.index equals node1.index
	join node2 in Query.Split('|')[2].Split('=')[1].Split(',').Select((item, index) => new { item, index })
		on node0.index equals node2.index
		select
			new XElement("stud",
				new XElement(Query.Split('|')[0].Split('=')[0], node0.item),
				new XElement(Query.Split('|')[1].Split('=')[0], node1.item),
				new XElement(Query.Split('|')[2].Split('=')[0], node2.item)
			)
);

Basically modified the structure to loop via the comma delimited Values rather than the Names (ID, class, name).
The tricky bit is getting the lists to join by the list index.
Here is link if you are interested: http://techbrij.com/linq-combine-multiple-lists-parallel-c[^]
C#
//Here is an alternative method, this one caters for additional names, for example additional grade in the var Query:
var Query = "ID=265465,265466,265467,265468" + "|" +
"class=6,6,6,6" + "|" +
"grade=A,B,C,D" + "|" +
"name=ABC,DEF,GHI,JKL";

var query = new XElement("Students",
	from node0 in Query.Split('|')[0].Split('=')[1].Split(',').Select((item, index) => new { item, index })
		select
			new XElement("stud",
				from n in Query.Split('|')
					select new XElement(n.Split('=')[0], n.Split('=')[1].Split(',')[node0.index].Trim())
			)
);
 
Share this answer
 
v2
Comments
KaushalJB 7-Nov-14 6:53am    
+5 Good Job
jaket-cp 7-Nov-14 6:56am    
thanks :)
Member 10359492 7-Nov-14 7:13am    
Thanks a lot much dude...
5+ yar,
You are life saver man. Thanks
jaket-cp 7-Nov-14 7:18am    
no problem
Maciej Los 7-Nov-14 7:37am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900