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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:57:36+00:00 2026-06-08T07:57:36+00:00

I’m trying to figure out how to use XmlTextReader and XmlTextWriter for the configuration

  • 0

I’m trying to figure out how to use XmlTextReader and XmlTextWriter for the configuration file of my program(s).

The xml file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Base>
    <Global>
        <some_config_value>86400</some_config_value>
        <test1>t_1</test1>
        <test2>t_2</test2>
        <test3>t_3</test3>
        <test4>t_4</test4>
    </Global>
    <test_head>
        <test5>t_5</test5>
        <test6>t_6</test6>
    </test_head>
</Base>

And here is the class I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace my_program.Global
{
    class CXMLConfig
    {
        private string path;

        public CXMLConfig(string filepath)
        {
            path = filepath;
        }

        public string XmlReadValue(string Section, string Key)
        {
            XmlTextReader textReader = new XmlTextReader(path);
            ReadElements readEl = new ReadElements(textReader, Section, Key);
            textReader.Close();
            return readEl.Value;
        }

        private class ReadElements
        {
            XmlTextReader textReader;
            string Section;
            string Key;

            private bool inBase = false;
            private bool inSection = false;
            private bool inKey = false;

            public string Value { get; private set; }

            public ReadElements(XmlTextReader textReader_set, string Section_set, string Key_set)
            {
                Value = "";

                this.textReader = textReader_set;
                this.Section = Section_set;
                this.Key = Key_set;

                textReader.Read();
                while (textReader.Read())
                {
                    // Move to fist element
                    textReader.MoveToElement();

                    string nodetype = textReader.NodeType.ToString();

                    if (textReader.LocalName == "Base")
                    {
                        if (nodetype == "Element")
                        {
                            if (!inBase)
                                inBase = true;
                        }
                        else if (nodetype == "EndElement")
                        {
                            if (inBase)
                                inBase = false;
                        }
                    }
                    else if (inBase && textReader.LocalName == Section)
                    {
                        if (nodetype == "Element")
                        {
                            if (!inSection)
                                inSection = true;
                        }
                        else if (nodetype == "EndElement")
                        {
                            if (inSection)
                                inSection = false;
                        }
                    }
                    else if (inBase && inSection && textReader.LocalName == Key)
                    {
                        if (inSection)
                        {
                            if (nodetype == "Element")
                            { 
                                if (!inKey)
                                    inKey = true;
                            }
                            else if (nodetype == "EndElement")
                            {
                                if (inKey)
                                    inKey = false;
                            }
                        }
                    }
                    else if (inBase && inSection && inKey)
                    {
                        if (nodetype == "Text")
                        {
                            Value = textReader.Value.ToString();
                            //Console.WriteLine(Value);
                        }
                    }
                }
            }

        }
    }
}

So first of all, this is probably bad XML.. I have never used it before and it does look a little.. odd. And then there is the fact that I wrote this whole ReadElements class to read a value out of the config file, but I imagined there would be a much simpler way to do this with XmlTextReader (but I couldn’t find it). And then lastly, I have yet to figure out how to update a value in the xml file using XmlTextWriter without re-writing the whole xml file from top to bottom.

  • 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-08T07:57:39+00:00Added an answer on June 8, 2026 at 7:57 am

    You could use XmlSerializer to serialize and deserialize an arbitrary configuration class. Here is a sample implementation:

    public enum MyEnum
    {
        ValueA,
        ValueB
    }
    
    [Serializable]
    public class Configuration : PersistableObject
    {
        public double A { get; set; }
        public string B { get; set; }
        public MyEnum C { get; set; }
    }
    
    public class PersistableObject
    {
        public static T Load<T>(string fileName) where T : PersistableObject, new()
        {
            T result = default(T);
    
            using (FileStream stream = File.OpenRead(fileName))
            {
                result = new XmlSerializer(typeof(T)).Deserialize(stream) as T;
            }
    
            return result;
        }
    
        public void Save<T>(string fileName) where T : PersistableObject
        {
            using (FileStream stream = new FileStream(fileName, FileMode.CreateNew))
            {
                new XmlSerializer(typeof(T)).Serialize(stream, this);
            }
        }
    }
    

    For more information on how to configure XmlSerializer, have a look at the MSDN article: http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.