【发布时间】: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