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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:21:48+00:00 2026-05-31T02:21:48+00:00

I am writing a function to update my XML and am having some issues.

  • 0

I am writing a function to update my XML and am having some issues. I’d hoped I could just set old XElement directly to the updated one but that doesn’t work in the foreach loop (though should only ever be one query result). I was going to hard-code each field, but then decided to use a loop. However when it hits the section and it’s sub-elements it reads it all as a single element and messed up the file

var myDevice = from c in appDataXml.Descendants("device")
               where (string)c.Element("name") == App.CurrentDeviceName 
                      && (string)c.Element("ip") == App.CurrentIp
               select c;

if (myDevice.Any())
{
    foreach (XElement device in myDevice)
    {
        //device.Element("customname").Value = updatedDevice.Element("customname").Value;

        for (int a = 0; a < device.Elements().Count(); a++)
        {
            string field = device.Elements().ElementAt(a).Name.ToString();
            device.Element(field).Value = updatedDevice.Element(field).Value;
        }
    }
}

sample XML

<Devices>
  <device>
    <name>blah</name>
    <customname>custom</customname>
    <ip>192.168.1.100</ip>
    <port>1000</port>
    <sources>
      <source>
        <code>00</code>
        <name>blah2</name>
        <hidden>False</hidden>
      </source>
      <source>
      ...etc
    </sources>
  </device>
  • 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-31T02:21:49+00:00Added an answer on May 31, 2026 at 2:21 am

    You need to abstract (make classes) of your xml, it will help you CRUD and search it.

    Example: (using these extensions: http://searisen.com/xmllib/extensions.wiki)

    public class Device
    {
        const bool ELEMENT = false;
        const bool ATTRIBUTE = true;
    
        XElement self;
        public Device(XElement self) { this.self = self; }
    
        public string CustomName 
        { 
            get { return self.Get("customname", string.Empty); }
            set { self.Set("customname", value, ELEMENT); }
        }
        public string Name { get { return self.Get("name", string.Empty); } }
        public string Ip { get { return self.Get("ip", "0.0.0.0"); } }
        public int Port { get { return self.Get("port", 0); } }
    
        public Source[] Sources
        {
            get { return _Sources ?? (_Sources = self.GetEnumerable("sources/source", xsource => new Source(xsource)).ToArray()); }
        }
        Source[] _Sources;
    
        public class Source
        {
            XElement self;
            public Source(XElement self) { this.self = self; }
    
            public string Code { get { return self.Get("code", string.Empty); } }
            public string Name { get { return self.Get("name", string.Empty); } }
            public bool Hidden { get { return self.Get("hidden", false); } }
        }
    }
    

    An example using it:

    XElement xdevices = XElement.Load(file.FullName);
    Device[] devices = xdevices.GetEnumerable("device", xdevice => new Device(xdevice)).ToArray();
    var myDevice = devices
         .Where(d => d.Name == App.CurrentDeviceName 
                  && d.Ip == App.CurrentIp);
    
    foreach (Device device in myDevice)
    {
        string name = device.Name;
        foreach (Device.Source source in device.Sources)
        {
            string sourceName = source.Name;
        }
        device.CustomName = "new name";
    }
    xdevices.Save(file.FullName);
    

    It is a change in the way of thinking, so instead of worrying about how to reference a value, you create the class to read/write to the xml, and instead you just get/pass off data to a class, that then reads/writes the xml.

    Edit:
    —- Add to XElementConversions class —-

    To be consistent with the file, and to work properly I made detailed versions. You can modify them to make the other types, like bool, DateTime, etc.

    public static int GetInt(this XElement source, string name, int defaultValue)
    {
        source = NameCheck(source, name, out name);
        return GetInt(source, source.GetDefaultNamespace() + name, defaultValue);
    }
    
    public static int GetInt(this XElement source, XName name, int defaultValue)
    {
        int result;
        if (Int32.TryParse(GetString(source, name, null), out result))
            return result;
        return defaultValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck writing a simple form...I feel dumb. Here's my controller: function welcome_message(){ //Update
I am writing the C# function which retrieves some files matched pattern. Input :
I'm learning scala, and I'm looking to update a nested node in some xml.
Hallo, I'm having trouble writing a function: float turnToRequestedHeading(float initialHeading, float requiredHeading, float turnRate)
I'm writing a subroutine for DBI updates, and are having some trouble figuring out
Whats this syntax useful for : function(String... args) Is this same as writing function(String[]
I'm writing a function that fishes out the src from the first image tag
I'm writing a function for an installer DLL to verify the Authenticode signature of
I am writing a function in Haskell that deals with numbers beyond the length
I am writing a function short getBits(short data, int p, int n) I have

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.