これをどのように単純化できますか?
次のような XML ファイルがあります。
<Root>
<Header />
<Content />
<Content />
<Content />
<!-- …. Lots of Contents -->
<Footer />
</Root>
各ファイルに「ヘッダー」と「フッター」があり、次に X 個の「コンテンツ」があるように、小さなファイルに分割する必要があります。私の現在の方法では、メイン ファイルのコピーを取得し、すべてのコンテンツを削除してから、スキップ & テイクに基づいて必要なものを追加します。
毎回オリジナルをメモリにコピーし、何かを削除してから、削除したばかりのものをいくつか追加しているので、私はそれが好きではありません:
while (index < totalContents) {
var newXDoc = new XDocument(xDoc);
newXDoc.Descendants("Content").Remove();
newXDoc.Root.Element("Header").AddAfterSelf(
xDoc
.Descendants("Content")
.Skip(index)
.Take(ordersPerFile)
);
xmlDocs.Add(XmlConverter.ToXmlDocument(newXDoc));
index += ordersPerFile;
}
このタスクを実行するためのよりエレガントな方法はありますか?
私は次のようなものを探しています:
while (index < totalContents) {
var newXDoc = new XDocument(
xDoc.Where(x => x.ContentElementsThatIWantForThisIteration...)
);
xmlDocs.Add(XmlConverter.ToXmlDocument(newXDoc));
index += ordersPerFile;
}
ありがとう