String Builder を使用して XML 形式のファイルを生成しています。ファイルは次のようになります:-
StringBuilder strBldr = new StringBuilder();
strBldr.AppendLine("<Root>");
strBldr.AppendLine("<ProductDetails>");
strBldr.AppendLine("<PId>" + lblproductid.Text + "</PId>");
strBldr.AppendLine("<PDesc>" + strtxtProductDesc + "</PDesc>");
strBldr.AppendLine("</ProductDetails>");
strBldr.AppendLine("</Root>");
これは for ループ内にあるため、多くの製品の詳細が含まれる場合があります。この文字列が制限された長さ (100 と仮定) を超える場合は、この文字列を分割する必要があります。これまでは簡単でした。次の方法を使用してこれを分割できます:-
public static IEnumerable<string> SplitByLength(this string str, int maxLength)
{
for (int index = 0; index < str.Length; index += maxLength)
{
yield return str.Substring(index, Math.Min(maxLength, str.Length - index));
}
}
しかし、問題は、このメソッドは、長さが 100 を超えていることがわかった場合、単に文字列を分割するだけです<ProductDetails>
。そこの。これを実現するには、コードに何を追加すればよいですか?