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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:27:01+00:00 2026-05-16T20:27:01+00:00

I was working on a bunch of XMLs that all share an attribute that

  • 0

I was working on a bunch of XMLs that all share an attribute that contains the string “name” in them. The following code selects the attribute with string “name” in it and assign a new value to it.

        public void updateXmlFile(string strFileName)
    {
        try
        {
            //Load the Document
            XmlDocument doc = new XmlDocument();
            doc.Load(strFileName);
            //Set the changed Value
            string newValue = GetUniqueKey();
            //Select all nodes in the XML then choose from them
            //the first node that contain string "name" in it
            XmlNodeList list = doc.SelectNodes("//@*");
            XmlNode filteredNode = list.Cast<XmlNode>()
                .First(item => item.Name.ToLower().Contains("name"));
            //Assign the newValue to the value of the node
            filteredNode.Value = newValue;

            doc.Save(strFileName);
        }
        catch (XmlException xex) { Console.WriteLine(xex); }
    }

Now a new XMLs were added that dosen’t have the string “name” in them, so instead of modifying the attribute with string “name” in it I decided to simply modify the last attribute no matter what it was (not the first)

Can anybody tell me how to do that?

EDIT

Here is an example of my XML

<?xml version="1.0" encoding="utf-8"?>
<CO_CallSignLists Version="24" ModDttm="2010-09-13T06:45:38.873" ModUser="EUADEV\SARE100" ModuleOwner="EUADEVS06\SS2008" CreateDttm="2009-11-05T10:19:31.583" CreateUser="EUADEV\A003893">
  <CoCallSignLists DataclassId="E3FC5E2D-FE84-492D-AD94-3ACCED870714" EntityId="E3FC5E2D-FE84-492D-AD94-3ACCED870714" MissionID="4CF71AB2-0D92-DE11-B5D1-000C46F3773D" BroadcastType="S" DeputyInSpecialList="1" SunSpots="1537634cb70c6d80">
    <CoCallSigns EntityId="DEBF1DDB-3C92-DE11-A280-000C46F377C4" CmdID="C45F3EF1-1292-DE11-B5D1-000C46F3773D" ModuleID="6CB497F3-AD63-43F1-ACAE-2C5C3B1D7F61" ListType="HS" Name="Reda Sabassi" Broadcast="INTO" PhysicalAddress="37" IsGS="1" HCId="0" CommonGeoPos="1" GeoLat="0.0000000" GeoLong="0.0000000">
      <CoRadios EntityId="E1BF1DDB-3C92-DE11-A280-000C46F377C4" RadioType="HF" />
    </CoCallSigns>
  </CoCallSignLists>
</CO_CallSignLists>

@Alex: You notice that the “SunSpots” attribute (last attribute in the first child element) is successfully changed. But now when I wanna load the XML back into the DB it gives me an error

Here is the modified code

    public void updateXmlFile(string strFileName)
    {
        try
        {
            XDocument doc = XDocument.Load(strFileName);

            XAttribute l_attr_1 = (doc.Elements().First().Elements().First().Attributes().Last());
            l_attr_1.Value = GetUniqueKey();

            Console.WriteLine("Name: {0}  Value:{1}", l_attr_1.Name, l_attr_1.Value);

            doc.Save(strFileName);
        }
        catch (XmlException xex) { Console.WriteLine(xex); }
    }

I was thinking of making an if statment which checks if the XML has an attribute that contains string “name” in it (since most of my XMLs has an attribute that contains name in them) if it does then change the attribute’s value if not look for the last attribute and change it.. not the best solution but just throwing it out there

  • 1 1 Answer
  • 1 View
  • 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-16T20:27:02+00:00Added an answer on May 16, 2026 at 8:27 pm

    Then definitely use Linq to XML.
    Example:

    using System.Xml.Linq;
    
    string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
    <Commands Version=""439""  CreateUser=""Reda"">
      <CmCommands DataclassId=""57067ca8-ef96-4d2e-a085-6bd7e8b24126"" OrderName = ""Tea"" Remark=""Black"">
        <CmExecutions EntityId=""A9A5B0F2-6AB4-4619-9106-B0F85F86EE01"" Lock=""n"" />
      </CmCommands>
    </Commands>";
    
    XDocument x = XDocument.Parse(xml);
    
    Debug.Print(x.Elements().First().Elements().First().Attributes().Last().Value);
    //                 Commands ^      CmCommands ^             Remark ^
    

    That is, word for word, the last attribute of the first child of the first element.

    You can also query for element/attribute names, like:

    Debug.Print(x.Descendants(XName.Get("CmCommands", "")).First().Attribute(XName.Get("Remark", "")).Value);
    

    And of course you can use all of the Linq goodness like Where, Select, Any, All etc.

    Note: replace XDocument.Parse with XDocument.Load if appropriate etc.

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

Sidebar

Related Questions

I'm working on an application that intersperses a bunch of jython and java code.
I am working on a django application that contains a bunch of related objects.
I'm working with some code that does a bunch of asynchronous operating with various
Working on a bunch of forms at the moment and I'm finding that I
I'm working with a database that has a bunch of serial numbers that are
I am working on this Tridion implementation that has a bunch of very different
Right now I'm working on a program that throws up a bunch of separate
I'm working on an algorithm that does prettymuch the same operation a bunch of
I am working on a form with a bunch of selection criteria that will
I'm working on a multimethod that needs to update a hash for a bunch

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.