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

  • Home
  • SEARCH
  • 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 731841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:04:26+00:00 2026-05-14T07:04:26+00:00

I have an XDocument that looks similar to <root> <a> <b foo=1 bar=2 />

  • 0

I have an XDocument that looks similar to

<root>
     <a>
          <b foo="1" bar="2" />
          <b foo="3" bar="4" />
          <b foo="5" bar="6" />
          <b foo="7" bar="8" />
          <b foo="9" bar="10" />
     </a>
</root>

I wish to change the attribute foo to something else, and the attribute bar to something else. How can I easily do this? My current version (below) stack overflows with large documents, and has an awful smell to it.

        string dd=LoadedXDocument.ToString();
        foreach (var s in AttributeReplacements)
            dd = dd.Replace(s.Old+"=", s.New+"=");
  • 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-14T07:04:26+00:00Added an answer on May 14, 2026 at 7:04 am

    Here is a complete XSLT solution:

    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:my="my:reps"
        exclude-result-prefixes="my"
    >
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
        <my:replacements>
          <foo1 old="foo"/>
          <bar1 old="bar"/>
        </my:replacements>
    
        <xsl:variable name="vReps" select=
         "document('')/*/my:replacements/*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="@*">
      <xsl:variable name="vRepNode" select=
       "$vReps[@old = name(current())]"/>
    
       <xsl:variable name="vName" select=
        "name(current()[not($vRepNode)] | $vRepNode)"/>
    
       <xsl:attribute name="{$vName}">
         <xsl:value-of select="."/>
       </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document, the desired result is produced:

    <root>
       <a>
          <b foo1="1" bar1="2"/>
          <b foo1="3" bar1="4"/>
          <b foo1="5" bar1="6"/>
          <b foo1="7" bar1="8"/>
          <b foo1="9" bar1="10"/>
       </a>
    </root>
    

    Do note that this is a generic solution, allowing any list of replacements to be specified and modified without modifying the code. The replacements can be in a separate XML file, for ease of maintenance.

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

Sidebar

Related Questions

I have a document that looks something like <root> <element> <subelement1 /> <subelement2 />
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
Say I have a xml document that looks like this <foo> <bar id=9 />
I have a XDocument that looks like this: XDocument outputDocument = new XDocument( new
Assume there exists an XML instance document that looks like this: <root> <object type=foo>
I have something that looks like the following document structure: public class Document {
I have an SOAP response that looks similar to this: <?xml version=1.0 encoding=UTF-8?> <soapenv:Envelope
I have an XML document (an InfoPath form) that looks similar to this: <my:ClientMaintenance
I have a text document that looks similar to this: R.D. P.N. X Y
I have a page with content that looks similar to this: <div id=level1> <div

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.