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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:24:55+00:00 2026-06-05T08:24:55+00:00

I have a xml file . I have to search for an attribute and

  • 0

I have a xml file . I have to search for an attribute and to replace its value with some value using c#
Further i dont know how many times does this attribute come and in how many elements as this xml is generated dynamically.
Any help over this?

  • 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-05T08:24:57+00:00Added an answer on June 5, 2026 at 8:24 am

    One way is to load the document into a System.Xml.XmlDocument instance, then find all occurrences of the respective attribute by using the SelectNodes method of the XmlDocument instance with an XPath expression and modify them accordingly.

    Here’s an example:

    Assume the following Xml document:

    <?xml version="1.0"?>
    <test>
        <a/>
        <b myAttribute="someValue"/>
        <c myAttribute="someOtherValue"/>
        <d/>
        <e>
            <f myAttribute="yetAnotherValue" anotherAttribute="anIrrelevantValue"/>
        </e>
    </test>
    

    Save the Xml document as test.xml. In the same directory, compile the following program. It will change the values of all attributes that are called myAttribute (selected by the XPath expression //@myAttribute):

    using System;
    using System.Xml;
    
    class Program
    {
        public static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("test.xml");
    
            Console.WriteLine("Before:");
            Console.WriteLine(doc.OuterXml);
    
            foreach (XmlNode node in doc.SelectNodes("//@myAttribute")) {
                node.Value = "new value";
            }
    
            Console.WriteLine("After:");
            Console.WriteLine(doc.OuterXml);
    
            doc.Save("test.xml");
    
            Console.ReadLine();
        }
    }
    

    (For your convenience, it also outputs the Xml document before and after the modification.)

    With Namespaces

    Now, the example is extended with namespaces (the XLink one, as requested by the OP):

    Xml file:

    <?xml version="1.0"?>
    <test xmlns:xlink="http://www.w3.org/1999/xlink">
        <a/>
        <b xlink:myAttribute="someValue"/>
        <c myAttribute="someOtherValue"/>
        <d/>
        <e>
            <f xlink:myAttribute="yetAnotherValue" anotherAttribute="anIrrelevantValue"/>
        </e>
    </test>
    

    C# code:

    using System;
    using System.Xml;
    
    class Program
    {
        public static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("test.xml");
    
            Console.WriteLine("Before:");
            Console.WriteLine(doc.OuterXml);
    
            XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
            nsMgr.AddNamespace("xlink", "http://www.w3.org/1999/xlink");
            foreach (XmlNode node in doc.SelectNodes("//@xlink:myAttribute", nsMgr)) {
                node.Value = "new value";
            }
    
            Console.WriteLine("After:");
            Console.WriteLine(doc.OuterXml);
    
            doc.Save("test.xml");
    
            Console.ReadLine();
        }
    }
    

    Remark 1: Note how only two occurrences of attributes called myAttribute are modified now, the third one (in the <c> element) does not belong to the namespace indicated in the XPath expression.

    Remark 2: The namespace prefix used in the Xml file and the C# code happens to be the same (xlink), but this is not required. You could, for example, use xl in the C# code instead and obtain the same result (only showing the changed lines):

    nsMgr.AddNamespace("xl", "http://www.w3.org/1999/xlink");
    foreach (XmlNode node in doc.SelectNodes("//@xl:myAttribute", nsMgr)) {
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML file that I am trying to search using Java. I
I have a huge XML file I need to do a Search and Replace
I have an XML file and I have a batch file to search for
I have found a XML file through google search that will help me for
I have an xml file say (test.xml) <root loc-ver=1.0> <data name=String1 xml:space=preserve> <value loc=root_data_value_2>Description
I have an options_menu.xml file like this: <?xml version=1.0 encoding=utf-8?> <menu xmlns:android=http://schemas.android.com/apk/res/android> <item android:id=@+id/search
i have xml file that looks like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE pointList SYSTEM
What is the easiest way to convert xml to html? I have xml file
I have an xml file with following structure: <table name=tblcats> <row> <Id>3680</Id> <Industry>Associations</Industry> <ParentId>1810</ParentId>
I have a XML file: <Root> <Lv1> <1_Data_Lv2_1>A1</1_Data_Lv2_1> <2_Data_Lv2_1>A2</2_Data_Lv2_1> </Lv1> <Lv1> <1_Data_Lv2_1>B1</1_Data_Lv2_1> <2_Data_Lv2_1>B2</2_Data_Lv2_1> </Lv1>

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.