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 6348787
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:29:25+00:00 2026-05-24T21:29:25+00:00

As I hack through our code base I just noticed this function. It converts

  • 0

As I hack through our code base I just noticed this function. It converts an IDictionary<string, object> (Paramters – an instance variable) into an XML string.

This is nothing but curiosity on my part 🙂 .

So can it be written with a lot less code using C# 4.0?Rule: no external libs except the .Net Framework BCL.

To make it more of a challenge I’m not putting the input dictionary spec here, as you should be able to work it out from the code.

public string ConvertToXml() {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<?xml version='1.0' encoding='utf-8'?><sc/>");
    foreach (KeyValuePair<string, object> param in Parameters) {
        XmlElement elm = doc.CreateElement("pr");

        if (param.Value is int || param.Value is Int32 || param.Value is Int16 || param.Value is Int64) {
            elm.SetAttribute("tp", "int");
        } else if (param.Value is DateTime?){
            elm.SetAttribute("tp", "datetime");
        } else {
            elm.SetAttribute("tp", "string");
        }

        elm.SetAttribute("nm", param.Key);
        if (param.Value is DateTime?) {
            DateTime? dateTime = param.Value as DateTime?;
            elm.SetAttribute("vl", dateTime.Value.ToString("o"));
        } else{
            elm.SetAttribute("vl", param.Value.ToString());
        }
        doc.FirstChild.NextSibling.AppendChild(elm);
    }
    return doc.OuterXml;
}

Let me add some more thoughts.

To me :

  • less is more but terse is bad
  • more types are fine, but trivial types seem smelly
  • reusability is good
  • 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-24T21:29:26+00:00Added an answer on May 24, 2026 at 9:29 pm

    Using dynamic and LINQ to XML:

    ConvertToXml can be reduced to one statement (assuming omitting the XML declaration is acceptable).

    public string ConvertToXml()
    {
        return new XElement("sc",
            Parameters.Select(param => CreateElement(param.Key, (dynamic)param.Value))
        ).ToString(SaveOptions.DisableFormatting);
    }
    

    Note that CreateElement casts param.Value to dynamic so that the correct overload from the following will be selected at runtime.

    XElement CreateElement(string key, object value)
    {
        return CreateElement("string", key, value.ToString());
    }
    
    XElement CreateElement(string key, long value)
    {
        return CreateElement("int", key, value.ToString());
    }
    
    XElement CreateElement(string key, DateTime value)
    {
        return CreateElement("datetime", key, value.ToString("o"));
    }
    

    The overloads above ultimately call:

    XElement CreateElement(string typename, string key, string value)
    {
        return new XElement("pr",
            new XAttribute("tp", typename),
            new XAttribute("nm", key),
            new XAttribute("vl", value)
        );
    }
    

    This code is reduces the number of statements (although not lines) found in the question. This approach builds on svick’s, but reduces the number of methods and dynamic calls required.

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

Sidebar

Related Questions

Ok this might be a bit of hack but bear with me :) The
I have tried this approach/hack: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/ The problem is this leaves a faint seam.
I've come across some old code <object id=foo classid=/location/bar.dll#ProjectName.ClassName viewastext></object> It doesn't currently work
In our production database, we ran the following pseudo-code SQL batch query running every
I am wondering if there is a way to complete this hack that I
I'm using a third-party library which accesses fonts through the GraphicsEnvironment: getAllFonts() call. This
We just upgraded our compiler to gcc 4.6 and now we get some of
I'm trying to hack my own dependency injection container in PHP, based on constructor
I have hack I need to employ under these conditions: It's the last page
From Wired magazine: ...the Palin hack didn't require any real skill. Instead, the hacker

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.