Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Edit Xml node

Posted on 2009-02-10
8
Medium Priority
?
787 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
0
Comment
Question by:Bill732
8 Comments
 

Author Comment

by:Bill732
ID: 23603186
Btw, it's a windows form app, not an ASP app.
Thanks!
0
 
LVL 96

Expert Comment

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

Expert Comment

by:MogalManic
ID: 23610222
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Assisted Solution

by:Dabas
Dabas earned 200 total points
ID: 23615160
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
0
 

Author Comment

by:Bill732
ID: 23623214
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?
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 1800 total points
ID: 23623262
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
0
 

Author Comment

by:Bill732
ID: 23623363
Great! I really appreciate it!
0
 

Author Closing Comment

by:Bill732
ID: 31545171
Thanks again.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

564 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