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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:15:30+00:00 2026-06-09T17:15:30+00:00

I am working on a c# console application which has an xml config file

  • 0

I am working on a c# console application which has an xml config file which contains the settings for the program.

I wanted to add a comment to the xml file to show what values you can use for a particular settings using <!--My Comment-->. For some reason when I put this in, its as if C# then thinks that this is end of the file and no other part of the file is read, there’s no errors being thrown and the program doesn’t stop responding it continues running the rest of the code.

Below is the config file.

<?xml version="1.0" encoding="utf-8" ?>
<options>
  <database>
    <item key="server" value="localhost" />
    <item key="database" value="emailserver" />
    <item key="username" value="myusername" />
    <item key="password" value="mypassword" />
    <item key="port" value="3306" />
  </database>
  <EmailServer>
    <item key="logFile" value="email_server.txt" />
    <!--You can use fileCopy or database-->
    <item key="logManageMode" value="fileCopy" />
    <item key="ip_address" value="127.0.0.1" />
    <item key="smtpPort" value="26" />
    <item key="requireAuthentication" value="false" />
  </EmailServer>
</options>

If I don’t put that comment in it reads in the entire file. Below is the code that reads the XML file.

public Dictionary<string, string> readConfig(string sectionName, bool soapService=false, Dictionary<string, string> config=null)
        {
            Dictionary<string, string> newConfig = null;
            if (config == null)
            {
                newConfig = new Dictionary<string, string>();
            }
            //Dictionary<string, string> config = new Dictionary<string, string>();
            try
            {
                XmlDocument configXml = new XmlDocument();
                string configPath = "";
                if (soapService)
                {
                    string applicationPath = HttpContext.Current.Server.MapPath(null);
                    configPath = Path.Combine(applicationPath, "config.xml");
                    configXml.Load(configPath);
                }
                else
                {
                    configXml.Load("config.xml");
                }

                XmlNodeList options = configXml.SelectNodes(string.Format("/options/{0}", sectionName));
                XmlNodeList parameters = configXml.GetElementsByTagName("item");
                foreach (XmlNode option in options)
                {
                    foreach (XmlNode setting in option)
                    {
                        string key = setting.Attributes["key"].Value;
                        string value = setting.Attributes["value"].Value;

                        if (config == null)
                        {
                            newConfig.Add(key, value);
                        }
                        else
                        {
                            config.Add(key, value);
                        }
                    }
                }
            }
            catch (KeyNotFoundException ex)
            {
                Console.WriteLine("Config KeyNotFoundException: {0}", ex.Message);
            }
            catch (XmlException ex)
            {
                Console.WriteLine("Config XmlException: {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Config Exception: {0}", ex.Message);
                Console.WriteLine("StackTrace: {0}", ex.StackTrace);
            }
            if (config == null)
            {
                return newConfig;
            }
            return config;
        }

Thanks for any help you can provide.

  • 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-09T17:15:31+00:00Added an answer on June 9, 2026 at 5:15 pm

    your comment is a node. so when you loop over the nodes:

     foreach (XmlNode option in options) 
                     { 
                         foreach (XmlNode setting in option) 
                         { 
                             string key = setting.Attributes["key"].Value; 
                             string value = setting.Attributes["value"].Value;
    

    you are tripping your try/catch block at the commebnt because that node does not contain the “key” or “value” attributes.

    You can use the NodeType property to determine if the node is a comment or not. For example:

     foreach (XmlNode option in options) 
                     { 
                         foreach (XmlNode setting in option) 
                         { 
                             if (setting.NodeType == XmlNodeType.Comment)
                             {
                                 continue;
                             }
                             string key = setting.Attributes["key"].Value; 
                             string value = setting.Attributes["value"].Value;
    

    Another option is to continue if the node is not an element:

    if (setting.NodeType != XmlNodeType.Element)
    

    As per Tomalak:
    Actually this is just as brittle as the OP’s original code. Insert a different node type into the XML than a comment and it will blow up again. Just select a specific XmlNodeList inside the loop:
    XmlNodeList settings = option.SelectNodes("item[@key and @value]");

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

Sidebar

Related Questions

I am working with a vb.net Windows Forms application which has Application Settings, running
I've got an app which I've tested as working as a console application. I've
I'm working on a console application which will be scheduled and run at set
I am working on one C# console application which create List collection of class
I'm working on a console application that tries to download some files from a
I m working on a console application that tries to download some files from
I am working on a console application to insert data to a MS SQL
Working with a Visual Basic.NET console application that features a VERY BASIC natural language
The following code is working great in a normal console application: private void button1_Click(object
I'm currently working on a small console application. This application is based on a

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.