C# 排序元素
我有以下XML文档(必要时可以重新设计),用于存储记录和错误C# 排序元素,c#,linq,.net-3.5,c#-3.0,linq-to-xml,C#,Linq,.net 3.5,C# 3.0,Linq To Xml,我有以下XML文档(必要时可以重新设计),用于存储记录和错误 <MYROOT> <RECORDS> <RECORD> <DATETIME>11/03/2010 14:12:41</DATETIME> <DOCUMENTID>1</DOCUMENTID> </RECORD> <RECORD> <DATETIME>
<MYROOT>
<RECORDS>
<RECORD>
<DATETIME>11/03/2010 14:12:41</DATETIME>
<DOCUMENTID>1</DOCUMENTID>
</RECORD>
<RECORD>
<DATETIME>11/03/2010 14:12:44</DATETIME>
<DOCUMENTID>2</DOCUMENTID>
</RECORD>
<RECORD>
<DATETIME>11/03/2010 14:12:45</DATETIME>
<DOCUMENTID>3</DOCUMENTID>
</RECORD>
</RECORDS>
<ERRORS>
<ERROR TYPE="ERR">
<DATETIME>11/03/2010 14:12:41</DATETIME>
<DETAIL>There has been a error on page 1</DETAIL>
</ERROR>
<ERROR TYPE="ERR">
<DATETIME>11/03/2010 14:13:03</DATETIME>
<DETAIL>There has been a error on page 101</DETAIL>
</ERROR>
<ERROR TYPE="SEQ">
<DATETIME>11/03/2010 14:13:03</DATETIME>
<DETAIL>Sequence Error, expected Sequence No. 101 Read 1</DETAIL>
</ERROR>
</ERRORS>
</MYROOT>
11/03/2010 14:12:41
1.
11/03/2010 14:12:44
2.
11/03/2010 14:12:45
3.
11/03/2010 14:12:41
第1页有一个错误
11/03/2010 14:13:03
第101页有个错误
11/03/2010 14:13:03
序列错误,预期序列号101读取1
我想输出记录和错误,但显然必须按日期排序,以便按顺序显示
如何按日期对它们进行排序,获取一组元素,然后对它们执行foreach循环?XDocument xml=System.xml.Linq.XDocument.Parse(您的xml);
var elements = doc.Descendants("RECORD").Concat(doc.Descendants("ERROR")).
OrderBy(x => DateTime.Parse(x.Element("DATETIME").Value));
foreach (XElement element in elements)
{
// do something interesting with element
}
IEnumerable records=xml.Root.Element(“records”).Elements();
IEnumerable errors=xml.Root.Element(“errors”).Elements();
IEnumerable elements=来自records.Concat中的el(错误)
Parse(el.Element(“DateTime”).Value)
选择el;
foreach(元素中的元素el)
{
//做点什么。
}
IEnumerable不是很灵活,最好的选择可能是从枚举表中删除元素,对它们进行排序并重新插入,保持正确的顺序(相对于以前的邻居)。如果子元素是排序键
这将从IEnumerable中删除命名元素,按子元素对它们进行排序(可能是您需要的,也可能不是您需要的),然后将它们重新插入正确的位置
private void SortIdNodes(XElement parent, String elementName, String sortElementname)
{
XNode prevElem = null;
XNode nextElem = null;
// Initial node count, to verify sets are equal
int initialElementsCount = parent.Descendants().Count();
List<XElement> unOrdered = parent.Descendants(elementName).ToList<XElement>();
if (unOrdered.Count() < 2){
return; // No sorting needed
}
// Make note of the neighbors
prevElem = unOrdered[0].PreviousNode;
nextElem = unOrdered.Last().NextNode;
// Remove set from parent
unOrdered.ForEach(el =>
{
el.Remove();
});
// Order the set, language (IEnumerable) semantics prevents us from changing order in place
List <XElement> ordered = unOrdered.OrderBy(x => x.Descendants(sortElementname).FirstOrDefault().Value).ToList<XElement>();
// Add to parent in correct order
if (prevElem != null) // If there's a first neighbor
{
ordered.ForEach(el =>
{
prevElem.AddAfterSelf(el);
prevElem = el;
});
}
else if (nextElem != null) // If there's only an end neighbor
{
ordered.Reverse();
ordered.ForEach(el =>
{
nextElem.AddBeforeSelf(el);
nextElem = el;
});
}
else // we're the only children of the parent, just add
{
ordered.ForEach(el =>
{
parent.Add(el); // add in order
});
}
int finalElementCount = parent.Descendants().Count();
if (initialElementsCount != finalElementCount)
{
throw new Exception("Error with element sorting, output collection not the same size as the input set.");
}
}
private void SortIdNodes(XElement父节点、String elementName、String sortElementname)
{
XNode prevElem=null;
XNode nextElem=null;
//初始节点计数,以验证集合是否相等
int initialElementScont=parent.subjections().Count();
List unOrdered=parent.substands(elementName.ToList();
if(无序.计数()<2){
return;//不需要排序
}
//记下邻居
prevElem=无序[0]。PreviousNode;
nextlem=unOrdered.Last().NextNode;
//从父级删除集合
无序。ForEach(el=>
{
el.移除();
});
//对集合进行排序,语言(IEnumerable)语义阻止我们在适当的位置更改顺序
List ordered=unOrdered.OrderBy(x=>x.substands(sortelElementName).FirstOrDefault().Value.ToList();
//按正确顺序添加到父级
if(prevElem!=null)//如果有第一个邻居
{
ordered.ForEach(el=>
{
prevElem.AddAfterSelf(el);
prevElem=el;
});
}
else if(nextElem!=null)//如果只有一个终端邻居
{
有序。反向();
ordered.ForEach(el=>
{
nextElem.AddBeforeSelf(el);
NEXTLEM=el;
});
}
否则//我们是父母唯一的孩子,只需添加
{
ordered.ForEach(el=>
{
parent.Add(el);//按顺序添加
});
}
int finalElementCount=parent.subjections().Count();
if(initialElementsCount!=finalElementCount)
{
抛出新异常(“元素排序错误,输出集合与输入集合大小不相同。”);
}
}
private void SortIdNodes(XElement parent, String elementName, String sortElementname)
{
XNode prevElem = null;
XNode nextElem = null;
// Initial node count, to verify sets are equal
int initialElementsCount = parent.Descendants().Count();
List<XElement> unOrdered = parent.Descendants(elementName).ToList<XElement>();
if (unOrdered.Count() < 2){
return; // No sorting needed
}
// Make note of the neighbors
prevElem = unOrdered[0].PreviousNode;
nextElem = unOrdered.Last().NextNode;
// Remove set from parent
unOrdered.ForEach(el =>
{
el.Remove();
});
// Order the set, language (IEnumerable) semantics prevents us from changing order in place
List <XElement> ordered = unOrdered.OrderBy(x => x.Descendants(sortElementname).FirstOrDefault().Value).ToList<XElement>();
// Add to parent in correct order
if (prevElem != null) // If there's a first neighbor
{
ordered.ForEach(el =>
{
prevElem.AddAfterSelf(el);
prevElem = el;
});
}
else if (nextElem != null) // If there's only an end neighbor
{
ordered.Reverse();
ordered.ForEach(el =>
{
nextElem.AddBeforeSelf(el);
nextElem = el;
});
}
else // we're the only children of the parent, just add
{
ordered.ForEach(el =>
{
parent.Add(el); // add in order
});
}
int finalElementCount = parent.Descendants().Count();
if (initialElementsCount != finalElementCount)
{
throw new Exception("Error with element sorting, output collection not the same size as the input set.");
}
}