【问题标题】:How can i first sort an XDocument and then save that XDocument by attribute我如何首先对 XDocument 进行排序,然后按属性保存该 XDocument
【发布时间】:2014-01-22 09:37:07
【问题描述】:

我有一个如下的 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");
        }
    }
}

任何建议都是可以理解的。谢谢。

【问题讨论】:

    标签: xml linq c#-4.0 linq-to-xml


    【解决方案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")));
    }       
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 2011-02-17
      • 2019-06-26
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多