We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Edit Xml node

Bill732
Bill732 asked
on
Medium Priority
795 Views
Last Modified: 2012-05-06
Hello,

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>:

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

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,
-Bill
Comment
Watch Question

Author

Commented:
Btw, it's a windows form app, not an ASP app.
Thanks!
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Have you tried using XSLT?  Are you using an XmlDocument?
What you need to use is the XmlReader.
http://msdn.microsoft.com/en-us/library/9d83k261(VS.80).aspx

So your code would look something like this:

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

Open in new window

CERTIFIED EXPERT
Commented:
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

Dabas

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
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?
XmlReader and XmlWriter both have a static Create() method that will create the correct implementation of the XmlReader/Writer
  http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.create(VS.80).aspx
  http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.create(VS.80).aspx

Author

Commented:
Great! I really appreciate it!

Author

Commented:
Thanks again.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.