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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:23:48+00:00 2026-05-30T23:23:48+00:00

I have a xml file which I modify with the following code: XmlDocument xlDoc

  • 0

I have a xml file which I modify with the following code:

 XmlDocument xlDoc = new XmlDocument();
            string[] files = Directory.GetFiles("C:\\Program Files (x86)", "Product.config", SearchOption.AllDirectories);
            string sfile = files[0];
            xlDoc.Load(sfile);
            XmlNodeList list = xlDoc.SelectNodes("//dependancy");
  XmlNode foundNode = xlDoc.SelectSingleNode("//dependancies//dependancy[@name='Microsoft Windows NT']");
int found = list.Count;

  if (foundNode == null)
  {
      foundNode = xmlElement;
      list[found - 1].AppendChild(foundNode);
      xlDoc.Save(sfile);
      ConfigurationManager.RefreshSection(sfile);
  }

  foreach (XmlNode node in list)
  {
      xlDoc.Load(sfile);
      try
      {//use inserted node}
      ...

Now before the foreach is executed , I want to reload the XML document, so that the value is in the document. The problem I am experiencing is that the changes are only loaded if the application is restarted. How can I reload the file before moving on to the foreach segment?

  • 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-30T23:23:49+00:00Added an answer on May 30, 2026 at 11:23 pm

    That’s some pretty confusing code; I assume xmlElement is a <dependancy name='Microsoft Windows NT'/> element you have created elsewhere.

    // in XPath expressions search all elements, so //dependancies//dependancy[@name='Microsoft Windows NT'] is the same as //dependancy[@name='Microsoft Windows NT']. Assuming there is already at least one <dependancy .../> element, then

    list[found - 1].AppendChild(foundNode);
    

    Will insert your xmlElement as a child inside the last existing <dependancy .../> element; like this:

    <dependancies>
        <dependancy>
            <dependancy name="Microsoft Windows NT" />
        </dependancy>
    </dependancies>
    

    That must be what you have, otherwise list[found - 1] will crash.

    The problem I am experiencing is that the changes are only loaded if the application is restarted.

    That is not correct. You already have the change in list[found - 1]. The real problem is that is not what you are searching:

    foreach (XmlNode node in list)
    

    That searches all children of list, not children of list[found - 1]. I am guessing that what you want is:

    <dependancies>
      <dependancy />
      <dependancy name="Microsoft Windows NT" />
    </dependancies>
    

    To get that, replace XmlNodeList list = xlDoc.SelectNodes("//dependancy"); with:

    XmlNode dependancies = xlDoc.SelectSingleNode("//dependancies");
    

    Replace list[found - 1].AppendChild(foundNode) with:

    dependancies.AppendChild(foundNode)
    

    And foreach (XmlNode node in list) with:

    foreach (XmlNode in dependancies.ChildNodes)
    

    If that is not what you want, please clarify.

    The reason you are getting your new xmlElement when you run the program again is because xlDoc.SelectNodes("//dependancy") searches all nodes, including those in <dependancy .../>.

    Also, what does “dependancy” mean? Do you mean “dependency”? Misspellings are very, very confusing.

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

Sidebar

Related Questions

I have a small C# program to modify a xml file which is located
I have a large XML file which in the middle contains the following: <ArticleName>Article
I have a custom XML file format which can contain blocks of code within
I have an XML file which I need to parse using PHP and send
I have an XML file which XML parser choke on. A part of it
I have an XML file which has many section like the one below: <Operations>
I have an XML file which I'd like to parse into a non-XML (text)
Good morning. I have an XML file which contains lists of warning and errors
Im using c# .net , windows form application. I have a XML file which
We have a PageRoles xml file which contains the page path and the user

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.