【发布时间】:2018-03-07 15:58:22
【问题描述】:
我有一个字符串数组,格式如下(每个元素至少包含 3 个节点,名称为 xref,属性为 ref-type 和 rid)
<xref ref-type="bibr" rid="ref20">[20]</xref> <xref ref-type="bibr" rid="ref21">[21]</xref> <xref ref-type="bibr" rid="ref22">[22]</xref>
<xref ref-type="bibr" rid="ref2">[2]</xref>, <xref ref-type="bibr" rid="ref3">[3]</xref>, <xref ref-type="bibr" rid="ref4">[4]</xref>
<xref ref-type="bibr" rid="ref101">101</xref>, <xref ref-type="bibr" rid="ref102">102</xref>, <xref ref-type="bibr" rid="ref103">103</xref>, <xref ref-type="bibr" rid="ref104">104</xref>, <xref ref-type="bibr" rid="ref106">106</xref>
<xref ref-type="bibr" rid="ref11">[11]</xref> <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> <xref ref-type="bibr" rid="ref4">[4]</xref>
<xref ref-type="bibr" rid="ref11">[11]</xref> <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> <xref ref-type="bibr" rid="ref14">[14]</xref>
我正在尝试遍历数组的每个元素并找到 3 个或更多节点 xref,它们各自的 rid 属性值增加了 +1,不包括文本 rid,并将它们输出到控制台或一个文件。
我已经完成了
foreach (var element in xrefs)
{
XDocument xd = XDocument.Parse("<root>"+element+"</root>",LoadOptions.SetLineInfo);
int len = xd.Descendants("xref").Count();
var values = from El in xd.Descendants("xref").Take(len - 2)
where El.CompareNext() && El.ElementsAfterSelf().FirstOrDefault().CompareNext()
select El;
foreach (var value in values)
{
Console.WriteLine(new string('-',50)+"\r\n"+element+"\r\n");
}
}
其中xrefs 是数组,ElementsAfterSelf() 是一个方法,创建如下
static class T1
{
public static Boolean CompareNext(this XElement xe)
{
return Convert.ToInt16(xe.Attribute("rid").Value.Replace("ref", "")) + 1 == Convert.ToInt16(xe.ElementsAfterSelf().FirstOrDefault().Attribute("rid").Value.Replace("ref", ""));
}
}
现在它产生的结果是这样的
--------------------------------------------------
<xref ref-type="bibr" rid="ref20">[20]</xref> <xref ref-type="bibr" rid="ref21">[21]</xref> <xref ref-type="bibr" rid="ref22">[22]</xref>
--------------------------------------------------
<xref ref-type="bibr" rid="ref2">[2]</xref>, <xref ref-type="bibr" rid="ref3">[3]</xref>, <xref ref-type="bibr" rid="ref4">[4]</xref>
--------------------------------------------------
<xref ref-type="bibr" rid="ref101">101</xref>, <xref ref-type="bibr" rid="ref102">102</xref>, <xref ref-type="bibr" rid="ref103">103</xref> <xref ref-type="bibr" rid="ref104">104</xref> <xref ref-type="bibr" rid="ref106">106</xref>
--------------------------------------------------
<xref ref-type="bibr" rid="ref101">101</xref>, <xref ref-type="bibr" rid="ref102">102</xref>, <xref ref-type="bibr" rid="ref103">103</xref> <xref ref-type="bibr" rid="ref104">104</xref> <xref ref-type="bibr" rid="ref106">106</xref>
--------------------------------------------------
<xref ref-type="bibr" rid="ref11">[11]</xref>, <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> <xref ref-type="bibr" rid="ref4">[4]</xref>
--------------------------------------------------
<xref ref-type="bibr" rid="ref11">[11]</xref>, <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> <xref ref-type="bibr" rid="ref14">[14]</xref>
--------------------------------------------------
<xref ref-type="bibr" rid="ref11">[11]</xref>, <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> <xref ref-type="bibr" rid="ref14">[14]</xref>
下面的字符串写了两次,但我只想要一次,因为它是同一件事
<xref ref-type="bibr" rid="ref101">101</xref>, <xref ref-type="bibr" rid="ref102">102</xref>, <xref ref-type="bibr" rid="ref103">103</xref> <xref ref-type="bibr" rid="ref104">104</xref> <xref ref-type="bibr" rid="ref106">106</xref>
<xref ref-type="bibr" rid="ref11">[11]</xref>, <xref ref-type="bibr" rid="ref12">[12]</xref> <xref ref-type="bibr" rid="ref13">[13]</xref> <xref ref-type="bibr" rid="ref14">[14]</xref>
谁能帮忙?
这是我正在使用的sample xml file 和full code
我试图在一些由 逗号 或 逗号和空格并将它们写入日志文件。我试图识别的连续节点应该将它们各自的属性rid 值增加+1 减去文本ref。除了refX 之外,任何其他具有不同rid 值的xref 节点都不需要检查。
【问题讨论】:
-
ref106是怎么进来的?什么是“文字摆脱”?ElementsAfterSelf或CompareNext? -
你想输出什么“他们”?你为什么在正文中使用
foreach (value而不是value? -
@NetMage
CompareNextnotElementsAfterSelfmy bad ...顺便检查更新的问题..
标签: c# linq linq-to-xml