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

时间:2014-01-22 09:37:07

标签: 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\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");
        }
    }
}

任何建议都值得注意。谢谢。

1 个答案:

答案 0 :(得分:1)

你想的更容易:

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