Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 需要按类型顺序向xml中添加元素_C#_Xml_Linq To Xml - Fatal编程技术网

C# 需要按类型顺序向xml中添加元素

C# 需要按类型顺序向xml中添加元素,c#,xml,linq-to-xml,C#,Xml,Linq To Xml,我正在处理一个庞大的数据列表,并将其解析为xml。对于这个问题,请按照要处理的许多GB大小和数亿个实例的顺序来考虑。我从数据的初始列表开始,将其解析为一个唯一的哈希集,可以使用该哈希集消除重复项,然后将其解析为xml并保存。我最终得到了这样的结构 <class>... </class> <instance>... </instance> . . . <instance> </instance> <class> &

我正在处理一个庞大的数据列表,并将其解析为xml。对于这个问题,请按照要处理的许多GB大小和数亿个实例的顺序来考虑。我从数据的初始列表开始,将其解析为一个唯一的哈希集,可以使用该哈希集消除重复项,然后将其解析为xml并保存。我最终得到了这样的结构

<class>...
</class>
<instance>...
</instance>
.
.
.
<instance>
</instance>
<class>
</class>
<instance>
</instance>
这基本上就是我想要的,但是因为我没有像class1 class2这样的元素,所以它不是很完美。我的结局是这样的

<class>
     <id>1</id>
     ....
</class>
<class>
     <id>42789945</id>
<class>
<class>
     <id>363445297</id>
</class>...

1.
....
42789945
363445297
...
实际上,我希望这些类保持有序,而不是在第一个类之后倒转。我很确定我可以切换到addbefore并使用该实例来完成我想要的,但是由于第一个实例在更深的循环中出现,我担心这会影响性能。到目前为止,我的优化只在1-2分钟的范围内运行,但是如果我不小心的话,如果我深入到这个过程中,它会持续数小时

我的大问题是,在上一节课之后,但在第一个实例之前,我是否错过了一个更简单的方法,或者在实例之前添加它会对我造成伤害

谢谢
吉米

我在等待答案时做了一个测试。我会留下这个,以防其他人遇到这个问题,因为我没有发现类似的问题。当您使用AddBeforeSelf时,它会在第一次迭代时停止,即使之后有数百万次,因此切换到AddBeforeSelf不会给我的执行增加任何时间,因此我将坚持使用它,除非有人只是说整个方法是错误的

吉米试试这个

            XElement xmlTree = new XElement("Root", new XElement[] {
                new XElement("Child1", 1),
                new XElement("Child2", 2),
                new XElement("Child3", 3),
                new XElement("Child4", 4),
                new XElement("Child5", 5)
            });​
            XElement xmlTree = new XElement("Root", new XElement[] {
                new XElement("Child1", 1),
                new XElement("Child2", 2),
                new XElement("Child3", 3),
                new XElement("Child4", 4),
                new XElement("Child5", 5)
            });​