我如何首先对Xdocument进行排序,然后通过属性保存该Xdocument



我的XML文件为Bellow:

<?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 SettingsSumit choudharyDesktopatesteNET0114_NET_001.pdf" newBlock="True" page="0" fid="1" min="1" max="4" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_NET_006.pdf" newBlock="True" page="5" fid="3" min="5" max="7" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_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 SettingsSumit choudharyDesktopatesteNET0114_NET_001.pdf" newBlock="True" page="0" fid="3" min="1" max="4" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_NET_006.pdf" newBlock="True" page="5" fid="1" min="5" max="7" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_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 SettingsSumit choudharyDesktopatesteNET0114_NET_001.pdf" newBlock="True" page="0" fid="1" min="1" max="4" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_NET_006.pdf" newBlock="True" page="5" fid="2" min="1" max="4" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_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 SettingsSumit choudharyDesktopatesteNET0114_NET_006.pdf" newBlock="True" page="5" fid="1" min="5" max="7" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_NET_006.pdf" newBlock="True" page="5" fid="2" min="1" max="4" />
    <fragment docpath="C:Documents and SettingsSumit choudharyDesktopatesteNET0114_NET_001.pdf" newBlock="True" page="0" fid="3" min="1" max="4" />
  </Article>
</root>

我已经写了bellow的程序:

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");
        }
    }
}

任何建议都是可观的。谢谢。

您认为要容易得多:

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")));
}       

相关内容

最新更新