使用linq将逗号分隔的字符串转换为xml结构 [英] Convert comma seperated string to xml structure using linq

查看:63
本文介绍了使用linq将逗号分隔的字符串转换为xml结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello开发人员,



我想使用linq将逗号分隔的字符串转换为xml结构

My String变量为:



Hello developers,

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

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





输出:ID = 265465,265466,265467,265468 |

class = 6,6,6,6 |

name = ABC,DEF,GHI,JKL



我需要这个以下xml结构





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

I need this into following xml structure

<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

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

<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>





我想用linq做这件事

不是在c#循环。



请帮助我们完成我的工作...



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

Please help me guys to achieve my work...

推荐答案

我是ñ确定这是否是最好的方法,但它确实得到你正在寻找使用linq与xml的结果:

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:
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)
			)
);



基本上通过逗号分隔的值而不是名称(ID,类,名称)。

棘手的一点是通过列表索引获取列表加入。

如果您感兴趣,这里是链接:http://techbrij.com/linq-combine-multiple-lists-parallel-c [ ^ ]


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[^]

//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())
			)
);


这篇关于使用linq将逗号分隔的字符串转换为xml结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆