I am generating a XML format file using String Builder. the file will look something like this :-
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>");
This is in for loop, so it may contain many product details. Now I need to split this string if it exceeds the limited length suppose 100. Till now it was easy. I am able to split this using following method:-
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));
}
}
But the thing is, this method just simply splits the string if it finds length is greater that 100. but I need to be sure if it tries to split from middle of the xml node, then it should find the just above <ProductDetails>
node and split from there.
what should I add to the code to achieve this?