tags:

views:

16

answers:

1

I am populating an XML structure (using VB.NET) to pass to a web service. Here is how one piece of the structure is filled, and the rest of the structure is filled in a similar manner:

Private Shared Function GetSpecialties(ByVal specialties As System.Data.Linq.EntitySet(Of Provider.provider_specialty)) As XElement

    Return _
        New XElement( _
            "provider_specialties", _
                From s In specialties _
                Select New XElement( _
                    "provider_specialty", _
                    New XElement("external_provider_specialty_id", s.external_provider_specialty_id), _
                    New XElement("record_type_id", s.record_type_id), _
                    New XElement("effective_date", s.effective_date), _
                    New XElement("termination_date", s.termination_date), _
                    New XElement("specialty_code", s.specialty_code)))

End Function

Some pieces of the structure can contain up to 30 elements. Each record takes about .1 seconds to create, and it needs to create about 35,000 records, so the whole process takes about an hour.

Is there a quicker way to fill an XML structure like this?

+2  A: 

If you're simply interested in creating the output XML string, you should save enough time by using a XmlTextWriter (over a StringBuilder?) to produce the XML. That would avoid instantiating many new objects, simply putting the desired content into the stream.

John Fisher
I'm using XDocument.Save() to create the file, but it's populating the elements that's taking time, not the writing of the elements.
gfrizzle
@gfrizzle: My suggestion was exactly what you needed then. Instead of populating elements, write the XML directly to the file -- avoiding the element population.
John Fisher
@John Fisher: Oh, now I see. Very intriguing. I'm definitely going to give this a try. Thank you for bing insistent. :)
gfrizzle
@gfrizzle: No problem. :)
John Fisher