如何将一个 XmlNodeList 拆分为两个子 XmlNodeList?



如何将 XmlNodeList 拆分为两个较小的 XmlNodeList,其中一个大小为 N,另一个具有总大小 - N?

请参阅下面的示例以及我尝试使用的地方:

public static void Main(string[] args)
{
XmlDocument someDoc = new XmlDocument();
someDoc.LoadXml(@"<bananas>
<banana tasty='yes'></banana>
<banana tasty='very'></banana>
<banana tasty='amazing'></banana>
<banana tasty='mind-blowing'></banana>
<banana tasty='disgusting'></banana>
</bananas>");
XmlNodeList bananaNodeList = someDoc.SelectNodes("//banana");
eatSomeBananas(bananaNodeList, 2);
}
/** Splits a XmlNodeList into two XmlNodeList, first one is a slice from 0 to numberOfBananas-1, and the other slice is from numberOfBananas and onwards
*/
public static void eatSomeBananas(XmlNodeList subBananaNodeList, int numberOfBananas)
{
XmlNodeList bananasToEat = subBananaNodeList.Cast<XmlNode>().Take(numberOfBananas) as XmlNodeList; //Error down-casting - null!
if (bananasToEat == null)
Console.WriteLine("Error! Did not work");
/*else 
doSomethingHere(bananasToEat); */
XmlNodeList remainingBananas = subBananaNodeList.Cast<XmlNode>().Skip(numberOfBananas) as XmlNodeList; //Error down-casting - null!
eatSomeBananas(remainingBananas, numberOfBananas);
}

我试图对IEnumerable<XmlNode>XmlNodeList(因为前者继承自后者(——我相信这应该是一个向上的。难道我以后就不能把它放回XmlNodeList吗?但如果不是,为什么不呢?

之后我不应该把它放回 XmlNodeList 吗?但如果不是,为什么不呢?

否,因为从Skip返回的值不是XmlNodeList。它只被声明为一个IEnumerable<XmlNode>,我希望Skip实现可能会使用迭代器块......当然,如果SkipXmlNodeList有任何详细的了解,我会感到惊讶.Take将以完全相同的方式工作。

就我个人而言,我完全避免使用旧的XML API,而只使用LINQ to XML。这在 LINQ 中自然而然地发挥作用 - 并且通常是一个更好的 XML API,IMO。

请注意,您不必使用它 - 您可以将整个代码更改为使用IEnumerable<XmlNode>而不是XmlNodeList

public static void eatSomeBananas(IEnumerable<XmlNode> subBananaNodeList, int numberOfBananas)
{
IEnumerable<XmlNode> bananasToEat = subBananaNodeList.Take(numberOfBananas);
IEnumerable<XmlNode> remainingBananas = subBananaNodeList.Skip(numberOfBananas);
// Added condition to avoid infinite recursion
if (remainingBananas.Any())
{
eatSomeBananas(remainingBananas, numberOfBananas);
}
}

然后,您可以在调用该方法时调用Cast一次:

eatSomeBananas(bananaNodeList.Cast<XmlNode>(), 2);

下面是 LINQ 到 XML 版本,我更喜欢它:

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
public class Test
{
public static void Main(string[] args)
{
XDocument someDoc = XDocument.Parse(
@"<bananas>
<banana tasty='yes'></banana>
<banana tasty='very'></banana>
<banana tasty='amazing'></banana>
<banana tasty='mind-blowing'></banana>
<banana tasty='disgusting'></banana>
</bananas>");
IEnumerable<XElement> bananas = someDoc.Descendants("banana");
EatSomeBananas(bananas, 2);
}
public static void EatSomeBananas(IEnumerable<XElement> bananas, int numberOfBananas)
{
var bananasToEat = bananas.Take(numberOfBananas);
Console.WriteLine("Eating some bananas"); 
foreach (var element in bananasToEat)
{
var tasty = element.Attribute("tasty").Value;
Console.WriteLine($"Tasty: {tasty}");
}
Console.WriteLine("Eaten the bananas");
var remainingBananas = bananas.Skip(numberOfBananas);
if (remainingBananas.Any())
{
EatSomeBananas(remainingBananas, numberOfBananas);
}
}
}

请注意,对于生产实现,我会避免递归并可能定期实现结果 - 否则它每次都会从头开始迭代,跳过加载然后获取一些负载。

我只是转换为IQueryable并使用它,比XmlNodeList更容易使用。

public static void Main(string[] args)
{
XmlDocument someDoc = new XmlDocument();
someDoc.LoadXml(@"<bananas>
<banana tasty='yes'></banana>
<banana tasty='very'></banana>
<banana tasty='amazing'></banana>
<banana tasty='mind-blowing'></banana>
<banana tasty='disgusting'></banana>
</bananas>");
XmlNodeList bananaNodeList = someDoc.SelectNodes("//banana");
var allBananas = bananaNodeList.Cast<XmlNode>().AsQueryable();
eatSomeBananas(allBananas, 2);
}
public static void eatSomeBananas(IQueryable<XmlNode> subBananas, int numberOfBananas)
{
var bananasToEat = subBananas.Take(numberOfBananas);
var remainingBananas = subBananas.Skip(numberOfBananas);
Console.WriteLine(string.Format("Bananas to eat: {0}", bananasToEat.Count()));
Console.WriteLine(string.Format("Remaining bananas: {0}", remainingBananas.Count()));
if (!bananasToEat.Any())
Console.WriteLine("Error! Did not work (not enough bananas!)");
else 
eatSomeBananas(remainingBananas, numberOfBananas);
}

输出:

Bananas to eat: 2
Remaining bananas: 3
Bananas to eat: 2
Remaining bananas: 1
Bananas to eat: 1
Remaining bananas: 0
Bananas to eat: 0
Remaining bananas: 0
Error! Did not work (not enough bananas!)

相关内容

  • 没有找到相关文章

最新更新