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

  • Home
  • SEARCH
  • 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 8532435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:42:53+00:00 2026-06-11T09:42:53+00:00

I would really appreciate input on the following implementation. Goals are: produce an XElement,

  • 0

I would really appreciate input on the following implementation. Goals are: produce an XElement, avoid file system reads/writes as much as possble.

private XElement ProduceApiXml(KeyValuePair<string, ConfigScriptOutput> kvp)
{
    XElement returnValue = null;
    // load xsl file
    XslCompiledTransform ct = new XslCompiledTransform();
    string xslFile = Path.Combine(_assemblyDir, ConfigurationManager.AppSettings["ConfigOutputXslFile"]);
    ct.Load(xslFile);
    XmlReader xmlToTransform = XmlReader.Create(new StringReader(kvp.Value.ScriptStdOut));
    StringBuilder sb = new StringBuilder();
    StringWriter sw = new StringWriter(sb);
    XmlWriter xmlTransformed = new XmlTextWriter(sw);
    ct.Transform(xmlToTransform, xmlTransformed);

    returnValue = XElement.Parse(sb.ToString());
    return returnValue;
}

This is the first time I have used this XslCompiledTransform class, and I am thinking I could imporve on it. My real concern is should I be using something other than the StringBuilder -> StringWriter -> XmlWriter constructs to get to where I want to be? Seems like a lot of objects built up to do the task at hand – which does work. Right now, working wins, but I want to make it better.

I’d really appreciate those of you with the experience commenting constructively on what I have done here. A code review, if you will?

Thanks in advance for your time.

  • 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-06-11T09:42:54+00:00Added an answer on June 11, 2026 at 9:42 am

    If you want to populate an XElement then use

    XDocument result = new XDocument();
    using (XmlWriter xw = result.CreateWriter())
    {
      ct.Transform(xmlToTransform, xw);
    }
    returnValue = result.Root;
    

    there is no need to use a StringWriter and no need to use the legacy XmlTextWriter, the transformation can directly populate an XContainer.

    I would also wrap the use of XmlReader and StringReader into using blocks.

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

Sidebar

Related Questions

I would really appreciate any help with the following problem: I need to be
I would really appreciate any jquery expert help as a method I've used previously,
I would really appreciate it if some of you could help optimize my tables,
I would really appreciate if you could shed light on this problem. I have
I would really appreciate if you would take a look at this piece of
Ok I give up and would really appreciate it if you guys could cast
I'm very new to C++ and would really appreciate any and all help. I
I'm new to postgreSQL, so would really appreciate any pointers from the community. I
My multi-threading knowledge is still pretty rudimentary, so would really appreciate some pointers here.
I needed some help/pointers on a homework problem. I would really appreciate it if

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.