Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6705565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:25:07+00:00 2026-05-26T07:25:07+00:00

I have a a set of database results that I am looping through that

  • 0

I have a a set of database results that I am looping through that I want to write to XML. Essentially I have a List of vars where each var has a set of properties (i.e. List<image> with image having image.name, image.path, image.id, etc.)

I want to write each property for each image out to XML. I’m looping through the list of images in a parallel loop. This is how I have it set up:

    using(XMLWriter writer = XMLWriter.Create(outputfile)
    {
    Parallel.ForEach({
         writer.WriteStartElement("DOCUMENT");
         writer.WriteElementString("id", idvalue);
         writer.WriteElementString("name", namevalue);
         writer.WriteEndElement();

         writer.WriteStartElement("DOCUMENT");
         writer.WriteElementString("id", idvalue);
         writer.WriteElementString("path", pathvalue);
         writer.WriteEndElement();
});
    }

Nevermind the exact syntax…I’m going roughly from my head for illustration but I want to write a bunch of elements to a file. I’m wondering how the parallel threading is affecting the processing. It appears to be containing each loops iteration of writing the elements together; I half-expected there to be end elements interjected throughout the output file but that hasn’t been the case. MSDN says “Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.” but then also in reference to the WriteEndElement “closes one element and pops the corresponding namespace scope.”.

Am I just lucky so far that it appears to write everything out in order or is XmlWriter actually sort of thread-aware between WriteStartElement and WriteEndElement? I don’t really care what order the elements themselves are in as long as each element is correctly written. Is there any real documentation either way?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:25:08+00:00Added an answer on May 26, 2026 at 7:25 am

    This is not thread-safe and will probably fail more clearly for larger datasets.

    But you can’t expect any improvement from parallelism here. You’re writing to an I/O device and that is the bottleneck, not the CPU.

    So just use a normal foreach(...)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am try to looping through a database result set, the problem is that i
i have set up an SQLite database. it seems that when i run a
I have set of flat files (114 files) each file is named with database
If you have a set of tables in the database that strictly consist of
I have this script set up that echoes all relevant users in a database
I have an External List over a products table in our database. I want
I have a database set up with email and password, I'm trying to use
I have set up the following: Database class ($db) Pagination class ($paginator) I am
Suppose the following: I have a database set up on database.mywebsite.com , which resolves
I have an SQL database set up in which I would like to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.