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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:36:24+00:00 2026-05-13T17:36:24+00:00

I am overriding a method which has an XmlReader being passed in, I need

  • 0

I am overriding a method which has an XmlReader being passed in, I need to find a specific element, add an attribute and then either create a new XmlReader or just replace the existing one with the modified content. I am using C#4.0

I have investigated using XElement (Linq) but I can’t seem to manipulate an existing element and add an attribute and value.

I know that the XmlWriter has WriteAttributeString which would be fantastic but again I am not sure how it all fits together

I would like to be able to do something like — This is pseudo-code!

public XmlReader DoSomethingWonderful(XmlReader reader)
{
   Element element = reader.GetElement("Test");
   element.SetAttribute("TestAttribute","This is a test");
   reader.UpdateElement(element);
   return reader;
}
  • 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-13T17:36:25+00:00Added an answer on May 13, 2026 at 5:36 pm

    XmlReader/Writer are sequential access streams. You will have to read in on one end, process the stream how you want, and write it out the other end. The advantage is that you don’t need to read the whole thing into memory and build a DOM, which is what you’d get with any XmlDocument-based approach.

    This method should get you started:

    private static void PostProcess(Stream inStream, Stream outStream)
    {
        var settings = new XmlWriterSettings() { Indent = true, IndentChars = " " };
    
        using (var reader = XmlReader.Create(inStream))
        using (var writer = XmlWriter.Create(outStream, settings)) {
            while (reader.Read()) {
                switch (reader.NodeType) {
                    case XmlNodeType.Element:
                        writer.WriteStartElement(reader.Prefix, reader.Name, reader.NamespaceURI);
                        writer.WriteAttributes(reader, true);
    
                        //
                        // check if this is the node you want, inject attributes here.
                        //
    
                        if (reader.IsEmptyElement) {
                            writer.WriteEndElement();
                        }
                        break;
    
                    case XmlNodeType.Text:
                        writer.WriteString(reader.Value);
                        break;
    
                    case XmlNodeType.EndElement:
                        writer.WriteFullEndElement();
                        break;
    
                    case XmlNodeType.XmlDeclaration:
                    case XmlNodeType.ProcessingInstruction:
                        writer.WriteProcessingInstruction(reader.Name, reader.Value);
                        break;
    
                    case XmlNodeType.SignificantWhitespace:
                        writer.WriteWhitespace(reader.Value);
                        break;
                }
            }
        }
    }
    

    This is not quite as clean as deriving your own XmlWriter, but I find that it’s much easier.

    [EDIT]

    An example of how you would open two streams at once might be something like this:

    using (FileStream readStream = new FileStream(@"c:\myFile.xml", FileMode.OpenOrCreate, FileAccess.Read, FileShare.Write)) {
      using (FileStream writeStream = new FileStream(@"c:\myFile.xml", FileMode.OpenOrCreate, FileAccess.Write)) {
        PostProcess(readStream, writeStream);
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's difference between shadowing and overriding a method in C#?
This question is specifically related to overriding the equals() method for objects with a
I'm creating a class named TetraQueue that inherits System.Collections.Generic.Queue class overriding the Dequeue method,
I am making a custom form object in Django which has an overrided __init__
I need to extend a class, which is encapsulated in a closure. This base
I'm aiming to create a set of objects, each of which has a unique
For a project I am working on in ruby I am overriding the method_missing
When overriding the MembershipProvider and calling it directly, is there a way to fill
When overriding the equals() function of java.lang.Object, the javadocs suggest that, it is generally
I'm overriding WndProc, so I want to write code like if (m.Msg == WM_COMMAND)

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.