Xml 如何首先对XDocument排序,然后按属性保存该XDocument

Xml 如何首先对XDocument排序,然后按属性保存该XDocument,xml,linq,c#-4.0,linq-to-xml,Xml,Linq,C# 4.0,Linq To Xml,我有一个xml文件,如下所示: <?xml version="1.0" encoding="utf-8"?> <root> <Article index="0" section="aaa" id="1" headline=" Make HTML5 work today "> <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET01

我有一个xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Article index="0" section="aaa" id="1" headline="   Make  HTML5  work today ">
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_001.pdf" newBlock="True" page="0" fid="1" min="1" max="4" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="3" min="5" max="7" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="2" min="1" max="4" />
  </Article> 
    <Article index="1" section="aaa" id="2" headline=" fgdfgfgfhg">
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_001.pdf" newBlock="True" page="0" fid="3" min="1" max="4" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="1" min="5" max="7" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="2" min="1" max="4" />
  </Article>
</root>

我想根据每篇文章片段的fid对该文档进行排序。输出应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <Article index="0" section="aaa" id="1" headline="   Make  HTML5  work today ">
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_001.pdf" newBlock="True" page="0" fid="1" min="1" max="4" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="2" min="1" max="4" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="3" min="5" max="7" />
  </Article> 
    <Article index="1" section="aaa" id="2" headline=" fgdfgfgfhg">
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="1" min="5" max="7" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_006.pdf" newBlock="True" page="5" fid="2" min="1" max="4" />
    <fragment docpath="C:\Documents and Settings\Sumit choudhary\Desktop\ateste\NET0114_NET_001.pdf" newBlock="True" page="0" fid="3" min="1" max="4" />
  </Article>
</root>

我已经编写了以下程序:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace ShortArticleBasedOnSegmentId
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            XDocument xmlDoc = XDocument.Load(@"D:\\articles.xml");

            var temp = xmlDoc.DescendantNodes().OfType<XElement>();
            IEnumerable<XElement> art = from a in temp.Descendants()
                                        where (string)a.Name.LocalName == "Article"
                                        select a;
            for (int i = 0; i < art.Count(); i++)
            {
                IEnumerable<XElement> frag = from f in art.Descendants()
                                             where (string)f.Name.LocalName == "fragment"
                                             select f;//real order of fragments in Article
                IEnumerable<XElement> fragShort = from f in art.Descendants()
                                                  orderby (int)f.Attribute("fid")
                                                  where (string)f.Name.LocalName == "fragment"
                                                  select f;//sorted order of fragments in Article

                for (int j = 0; j < frag.Count(); j++)
                {
            //what logic should i use to replace unsorted order to sorted order of fragments in Article


                }
            }            
            xmlDoc.Save(@"D:articles.xml");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.Xml.Linq;
命名空间ShortArticleBasedOnSegmentId
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
XDocument xmlDoc=XDocument.Load(@“D:\\articles.xml”);
var temp=xmlDoc.degenantNodes(),of type();
IEnumerable art=来自临时子代()中的
其中(字符串)a.Name.LocalName==“文章”
选择一个;
对于(int i=0;i

任何建议都是可以接受的。谢谢。

这比你想象的要容易得多:

var arts = xmlDoc.Root.Elements("Article");

foreach (var art in arts)
{
    // get fragments
    // ToArray call is really important here
    // otherwise it would be evaluated again from your doc within Add call
    // and would found nothing, because of Remove method called before
    var fragments = art.Elements("fragment").ToArray();

    // remove them from article
    fragments.Remove();

    // add again, sorted
    art.Add(fragments.OrderBy(f => (int)f.Attribute("fid")));
}