Edit Xml node

Posted on 2009-02-10
Last Modified: 2012-05-06

I am trying to write a portion of a C# app that runs through an XML file and html encodes the text found in a specific node, <SectionText>:

<![CDATA[<br />This is the node that needs to be html encoded.<br/ ><br />]]>

I have the html encode part written. It encodes a given string.

I tried using a streamreader because the files can be large (one is 170MB). I was using the methods of readline and writeline to loop through and edit the nodes, but it soon became too complicated to capture the full or partial segments of the target node that might be in a line read by the streamreader.

I was thinking of trying Xpath, but thought the file size might be an issue.

Do you have suggestions on either using XPath or a better way to read through the file and get the target substrings, html encode them and replace the substring with the html encoded version?

Thanks very much,
Question by:Bill732

    Author Comment

    Btw, it's a windows form app, not an ASP app.
    LVL 96

    Expert Comment

    by:Bob Learned
    Have you tried using XSLT?  Are you using an XmlDocument?
    LVL 21

    Expert Comment

    What you need to use is the XmlReader.

    So your code would look something like this:

    using (XmlReader reader=XmlReader.Create(new StreamReader(FileName))) {
       while (reader.Read()) {
          EncodeResults(reader.ReadString());  //Read the text content of the element.

    Open in new window

    LVL 27

    Assisted Solution

    I agree with MogalManic
    The XmlReader is  forward reading only, hence there should be no memory issues whatsoever.
    The XmlDocument will give you more power, and I use it for quite fairly big documents (>10MB), without getting out of memory messages either


    Author Comment

    Sorry it took awhile to check back - I was pulled away yesterday. Thanks very much for responding so fast. The XmlReader looks like the way to go.

    When I was trying my previous approach with streamreader and streamwriter I would read a line and write a line to a temp file (making the htmlencode edit where needed along the way). Then I would overwrite the original file with the temp file.

    I believe that I need to take a slightly different approach with XmlReader and XmlWriter since it is an abstract class - is that right?
    LVL 21

    Accepted Solution

    XmlReader and XmlWriter both have a static Create() method that will create the correct implementation of the XmlReader/Writer

    Author Comment

    Great! I really appreciate it!

    Author Closing Comment

    Thanks again.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now