1

I am trying to use an XML writer to build an xml string into a string builder.

This is working but the string that is returned is cut off at 6134 characters.

From what I read the issue lies with the string builder and not the xml writer.

I've tried increasing it's capacity manually but that doesn't seem to have any effect.

Here is some sample code

StringBuilder xmlout = new StringBuilder(50000000);

using (XmlWriter w = XmlWriter.Create(xmlout))
    {
        w.WriteStartDocument();
        w.WriteStartElement("order");
        w.WriteAttributeString("Name", "Value");

        // more elements and attributes...

        w.WriteEndElement(); //order
        w.WriteEndDocument();
    }

String xml = xmlout.ToString();

I've put in a very high capacity just as a test.

Any help appreciated

Danny Seager
  • 23
  • 1
  • 5
  • Did you try Flush()ing your writer? – zmbq Jun 07 '13 at 09:00
  • 1
    @zmbq It's inside a `using` so flushing is not needed. – Matthew Watson Jun 07 '13 at 09:01
  • I assumed that, but I wanted to be sure... – zmbq Jun 07 '13 at 09:01
  • 1
    @DannySeager StringBuilder can have a huge capacity, so that's not the issue. Also, there's no need for you to set its initial capacity to `50000000`. There's no exceptions happening and being hidden are there? – Matthew Watson Jun 07 '13 at 09:03
  • Hope this helps : http://stackoverflow.com/questions/7547827/how-does-stringbuilders-capacity-change – garvit gupta Jun 07 '13 at 09:07
  • I just tested your code, and created a 2 MB string without problems. Setting the initial capacity of the `StringBuilder` would only help if you have problem allocating more memory while creating the string. Allocating a few kilobyte is normally no problem. So, the problem seems to be with how you use the string after it is created. How do you determine that the string is cut off? – Guffa Jun 07 '13 at 09:08
  • @zmbq, I added in a few manual xmlwriter() flushes and the string returned was larger (9177) but it was still not the entire result. – Danny Seager Jun 07 '13 at 09:22
  • @Guffa I determine the string is cut off by putting a break point in, stepping through the code and then hovering over the xml string after the value has been assigned and (clicking on the magnifying glass) view the string. – Danny Seager Jun 07 '13 at 09:23
  • @garvit gupta So basically what the other thread is saying is that the capacity should manage itself, this is how I believed it should work. – Danny Seager Jun 07 '13 at 09:23
  • @DannySeager: What version of Visual Studio are you using? When I try that in VS 2010 I see the entire string, but earlier versions might show only part of the string. – Guffa Jun 07 '13 at 09:28
  • @Guffa Visual Studio 2012, Framework version 4.5 – Danny Seager Jun 07 '13 at 09:34
  • @DannySeager: I can't repeat it when I test it. Can you post sample code that shows the problem? – Guffa Jun 07 '13 at 09:40

0 Answers0