Effectient way to encode CDATA elements

Ok, I'm reading data from a stream using a StreamReader. The data inside the 
stream is not xml, it could be anything.

Based on the input StreamReader I'm writing to an output stream using an 
XmlTextWriter. Basically, when all is said and done, the output stream 
contains data from the input stream wrapped in a element contained in a 
parent element.

My problem is twofold. Data gets read from the input stream in chunks, and 
the StreamReader class returns char[]. If data in the input stream contains 
a "]]>" it needs to be split across two CDATA elements. First, how do I 
search for "]]>" in a char array? And second, because I'm reading in chunks, 
the "]]>" substring could be split across two chunks, so how do I account 
for this?

I could probably convert the char[] to a string, and do a search replace on 
it. That would solve my first problem. On each read, I could also check to 
see if the last character was a "]", so that on the next read, if the first 
two characters are "]>" I would start a new CDATA section.

This hardly seems efficient because it involves converting the char array to 
a string, which means spending time to copy the data, and eating up twice 
the memory. Is there a more efficient way, both speedwise and memory wise?

 

0
nospam6568 (71)
2/13/2009 6:05:41 AM
dotnet.xml 7266 articles. 0 followers. Follow

2 Replies
918 Views

Similar Articles

[PageSpeed] 58

Jeremy wrote:

> Based on the input StreamReader I'm writing to an output stream using an 
> XmlTextWriter. Basically, when all is said and done, the output stream 
> contains data from the input stream wrapped in a element contained in a 
> parent element.
> 
> My problem is twofold. Data gets read from the input stream in chunks, 
> and the StreamReader class returns char[]. If data in the input stream 
> contains a "]]>" it needs to be split across two CDATA elements. First, 
> how do I search for "]]>" in a char array? And second, because I'm 
> reading in chunks, the "]]>" substring could be split across two chunks, 
> so how do I account for this?


If you want to write a CDATA section with XmlWriter then if you use the 
method WriteCData it should take care of enforcing a well-formed result 
by splitting up the text as needed.
Here is an example:

             XmlWriterSettings xws = new XmlWriterSettings();
             xws.Indent = true;
             using (XmlWriter xw = XmlWriter.Create(Console.Out, xws))
             {
                 xw.WriteStartDocument();
                 xw.WriteStartElement("root");
                 xw.WriteStartElement("foo");
                 xw.WriteCData("]]>");
                 xw.WriteEndElement();
                 xw.WriteStartElement("foo");
                 xw.WriteCData(new String(new char[] { ']', ']', '>' }));
                 xw.WriteEndElement();
                 xw.WriteEndElement();
                 xw.WriteEndDocument();
             }

The result is

<root>
   <foo><![CDATA[]]]]><![CDATA[>]]></foo>
   <foo><![CDATA[]]]]><![CDATA[>]]></foo>
</root>

So you should buffer your input as needed, then simply use one 
WriteCData() and that method takes care of writing out as many sections 
as needed.



-- 

	Martin Honnen --- MVP XML
	http://JavaScript.FAQTs.com/
0
mahotrash (1777)
2/14/2009 2:12:02 PM
The contents of the incomming stream stream could be anything, any size.  I 
can't just allocate a buffer to hold the entire contents it could eat up too 
many resources.

"Martin Honnen" <mahotrash@yahoo.de> wrote in message 
news:#pFf44qjJHA.5724@TK2MSFTNGP02.phx.gbl...
> Jeremy wrote:
>
>> Based on the input StreamReader I'm writing to an output stream using an 
>> XmlTextWriter. Basically, when all is said and done, the output stream 
>> contains data from the input stream wrapped in a element contained in a 
>> parent element.
>>
>> My problem is twofold. Data gets read from the input stream in chunks, 
>> and the StreamReader class returns char[]. If data in the input stream 
>> contains a "]]>" it needs to be split across two CDATA elements. First, 
>> how do I search for "]]>" in a char array? And second, because I'm 
>> reading in chunks, the "]]>" substring could be split across two chunks, 
>> so how do I account for this?
>
>
> If you want to write a CDATA section with XmlWriter then if you use the 
> method WriteCData it should take care of enforcing a well-formed result by 
> splitting up the text as needed.
> Here is an example:
>
>             XmlWriterSettings xws = new XmlWriterSettings();
>             xws.Indent = true;
>             using (XmlWriter xw = XmlWriter.Create(Console.Out, xws))
>             {
>                 xw.WriteStartDocument();
>                 xw.WriteStartElement("root");
>                 xw.WriteStartElement("foo");
>                 xw.WriteCData("]]>");
>                 xw.WriteEndElement();
>                 xw.WriteStartElement("foo");
>                 xw.WriteCData(new String(new char[] { ']', ']', '>' }));
>                 xw.WriteEndElement();
>                 xw.WriteEndElement();
>                 xw.WriteEndDocument();
>             }
>
> The result is
>
> <root>
>   <foo><![CDATA[]]]]><![CDATA[>]]></foo>
>   <foo><![CDATA[]]]]><![CDATA[>]]></foo>
> </root>
>
> So you should buffer your input as needed, then simply use one 
> WriteCData() and that method takes care of writing out as many sections as 
> needed.
>
>
>
> -- 
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/ 

0
nospam6568 (71)
2/16/2009 4:16:56 AM
Reply:

Similar Artilces:

Way to writing an email but sending later
Hi there Downloaded a Plugin for OL2003 which works very well. I can create an email and 'time' it to send at a given time.. BUT can this be done without a plugin? Merry Christmas ;) Glen. Compose your message, select options, the use the Do Not Deliver Before option. Native to Outlook and costs nothing. --� Milly Staples [MVP - Outlook] Post all replies to the group to keep the discussion intact. All unsolicited mail sent to my personal account will be deleted without reading. After furious head scratching, GlenC asked: | Hi there | | Downloaded a Plugin for OL2003 whi...

Is there a way..
We are looking to make a public folder (Calendar) available on a web page. They do not need to modify it, just view it. We are using Exchange Server 2003. Andy: You should just be able to provide users with a path to that public folder. Let's say the Calendar is called BossCalendar. The path would look something like this: http://servername/public/BossCalendar You can change the permissions to allow anonymous access, if users don't need to authenticate. However, you will also need to modify the Public virtual directory so that anonymous authentication is allowe...

Is there a way to sort multiple columns with a tab or something?
I have a 4 column spreadsheet. I want to be able to click the heading for each of the columns to hav them sort by that column if clicked. How do I do that -- Message posted from http://www.ExcelForum.com Hi why not used the soprt icon for this. Note: This could screw up your sorting if Excel does not recognize your database columns correctly -- Regards Frank Kabel Frankfurt, Germany > I have a 4 column spreadsheet. > > I want to be able to click the heading for each of the columns to have > them sort by that column if clicked. How do I do that? > > > --- > Mess...

Provide a way to turn off auto-checking excel formulas as I type t
I am constantly running into this horrible design decision to auto-check excel formulas as they are typed. I type in formulas, then add parentheses from the right hand side back to the left to make certain of the calculation orders. Every time I put in a close paren and then move backwards to add the opening paren, Excel interrupts me and pops up a dialog telling me my formula is broken..... WHILE I AM STILL TYPING IT. The worst part is that it requires two clicks of non-default options that alternate positions (i.e. I have to move the mouse t the correct button two separate times) i...

Best way to display XML
Hello, i have a big XML file, i load the xml to a xmldocument. My question is, how is the best way to display de information (aspx page). I tryng load the xml into a dataset an then displaying into a grid, but the structure of the xml (combinations of the attributes and nodes) is dificult to take into a dataset, and not represent well de model of the xml. My xml not have allways the same structure, it depends to the parameter that i give a some webservice, and this webservice give a XML resultant to the query. note: I have de schema of the xml Only i need your opinions and experience. Th...

The fastest way to extract <blah></blah> from a file?
Potentially a giant file? jSmith wrote: > The fastest way to extract <blah></blah> from a file? > Potentially a giant file? sed, sort, a short Perl script ... that's how I'd do on my OS of choice, anyway :-/ CStringA text = ReallyHugeFile; int pos_1 = text.Find(0, "<blah>"); if (pos_1 != -1) { int pos_2 = text.Find(pos_1, "</blah>"); if (pos_2 != -1) return text.Mid(pos_1, pos_2 - pos_1); } -- - Mark Randall http://zetech.swehli.com "Cyde Weys" <cyde@umd.edu> wrote in message news:1125084619.261...

Re-installing
[This followup was posted to microsoft.public.outlook and a copy was sent to the cited author.] I just upgraded hard disks and installed Windows XP on my PC. I had saved my .PST file, so I thought it would be easy to bring Outlook 2002 back up like I had it before. I got all the messages back, but there were some problems. Oops 1: The PST file doesn't seem to have the account information in it it, so I had to re-enter it. Not a big deal, but could someone tell me if account info, view, etc can be saved for a re-install? Oops 2: Even though all my folders were there, my Rules a...

What is the best way to do this upgrade?
I have a client with CRM 1.2 running in one domain. They now want to upgrade to CRM 3.0 and move the CRM application to a box residing in a different domain. What is the best way to do this ? My idea is as below: Take back up of 1.2 databases (Production) Install SQL Server on another box in the same domain, restore 1.2 db, install CRM 1.2 pointing to existing database. Test the application thoroughly to check it is same as Production Upgrade to CRM3.0. Test again thoroughly. Take the backup of CRM3.0 database Install SQL Server with appropriate sp etc on a box in another domain Restore CRM...

What's the easiest way to insert a roster into publisher calendar
Select a calendar that you can include a "Schedule of events." Use this space for your roster. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "Mick Hoffmann" <Mick Hoffmann@discussions.microsoft.com> wrote in message news:D07FEC85-10C7-4009-8EA4-0F169CA989B1@microsoft.com... > ...

Merging two datasets deletes parent elements, preserves all children
Hi, My problem: Merging two datasets deletes parent elements, preserves all children. I've created two dataSets. Each use the same schema, parent-child nested tables. The first dataSet is loaded with historical data read from an XML file. The second dataSet has current data filled from a dataGrid. The system takes selected dataGrid data, dataSet updates a child element value, merges with the first dataSet, and finally writes out the latest version of the history file. Inside the file, there is a mix of parent table elements and children. All the children are preserved and correct, ...

Simplest Way to Deal With XML?
Dear : I need a simple way to deal with XML , like that found in .NET XmlDocument, is there any available class. Regards Have a look at www.codeguru.com there are some nice wrapper classes. M "Raed Sawalha" <raed_sawalha@hotmail.com> wrote in message news:uaO0IR5rEHA.644@tk2msftngp13.phx.gbl... > Dear : > > I need a simple way to deal with XML , like that found in .NET > XmlDocument, > is there any available class. > > Regards > > Use MSXML and the interfaces provided by it. It would definately help you to deal with XML. "M" &l...

is there a way to autoformat shading in all odd cell numbers?
is there a way to autoformat shading in all odd cell numbers? I'm not competely sure whether you mean odd rows or odd columns but would this help - http://www.cpearson.com/excel/banding.htm "tat2f" wrote: > is there a way to autoformat shading in all odd cell numbers? Conditional Formatting =MOD(ROW(),2)=1 "tat2f" wrote: > is there a way to autoformat shading in all odd cell numbers? tat2f wrote: > is there a way to autoformat shading in all odd cell numbers? Check out this freebie. Lots of real handy features http://www.asap-utilities.com/ gls858...

Effectient way to encode CDATA elements
Ok, I'm reading data from a stream using a StreamReader. The data inside the stream is not xml, it could be anything. Based on the input StreamReader I'm writing to an output stream using an XmlTextWriter. Basically, when all is said and done, the output stream contains data from the input stream wrapped in a element contained in a parent element. My problem is twofold. Data gets read from the input stream in chunks, and the StreamReader class returns char[]. If data in the input stream contains a "]]>" it needs to be split across two CDATA elements. First, how do ...

DRP
Hi all, I'm using Ex2003 standard, which is only supporting 25 users. I intend to set up a branch office as a disaster recovery site with some fast comms links and duplicate hardware. At present I have dedicated servers for DC's (2), SQL (1), Ex2003(1), Data (1), Terminal Services (1). Because the DR site only needs to support 3-5 users and it is likely we would relocate within 3 days of any disaster I am going to install a single server of very decent spec. i.e. dual xeon / terrabyte, 4gb ram etc. etc. My question is this. Is there an relatively straight forward way of ...

Best Way ?
G'day All, Looking for some information. With Outlook you can have all your contacts...address, email address, yada, yada, yada. Can I use this information in to say mail merge with Word and email a letter to the ppl with email addresses? Or is this better done with a Access? The reason I ask is that we want to deliver an email to all our clients with the latest promotions. And want it to be a bit more automated? Any information or pointers in the right direction would be appreciated. Have a good one, Scott V You can use either Access or Outlook as the data source for a ma...

A way to create sub-categories?
I want to group contacts by category and then a sub-category for easy viewing. Apart from creating a new field for sub-category is there a way of doing this? Outlook doesn't seem to support sub-categories. Thanks Nope. -- Patricia Cardoza Outlook MVP Author - Special Edition Using Microsoft Office Outlook 2003 Lead Author - Access 2003 VBA Programmer's Reference Author - Absolute Beginner's Guide to Microsoft OneNote 2003 http://blogs.officezealot.com/cardoza http://www.cardozasolutions.com "Casebase" <junkmail@casebase.net> wrote in message news:%23rx5a...

is there a way to crop a corner out of a rectangular image in Visi
Using subtract on two shapes accomplishes what i want to do, but when the first shape is a .gif image, it does not work. No, the "boolean operations", as they're called, do not operate on foreign objects like bitmaps and controls. Visio has crop functionality for imported graphics, but it is only rectangular. You could draw a small white triangle and give it "no line". Then place it over the corner of your bitmap. -- Hope this helps, Chris Roth Visio MVP "doug" <doug@discussions.microsoft.com> wrote in message news:29D479A6-6099-4051-B7A0-1...

Is there a way to color code Outlook Contact File Folder Labels?
Is there a way to color code Outlook Contact File Folder Labels? No. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Coauthor, OneNote 2003 for Windows (Visual QuickStart Guide) Author, Google and Other Search Engines (Visual QuickStart Guide) Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com Join OneNote Tips mailing list: http://www.onenote-tips.net/ "isailhbca" <isailhbca@discussions.microsoft.com> wrote in message news:4DBD10F7-FAA0-4102-A4C3-E2697908EFE8@microsoft.com... >...

is there a way to change the time card repor format?
i'm using activereports in RMS to generate a custome timecard report. for my timein and timeout colums, the datatype i have assigned to the columns is vbDate. when I generate the report, the columns only show the date as mm/dd/yyyy, they do not show the time of day piece for exactly when the cashier punched in or out. is there any way to change the formatting of this so that in the report the time shows up? for example, instead of showing 2/6/2005 in the time in column, it would show 2/5/2005 8:00:00 AM. thanks in advance, kevin There is a timecard report on the Reports Library...

Can You Count This Way?
I have some cells with note type data: For ex: ax,3b7,22,xy,zz, Is there a way to count every item between the commas? Thanks. Hi try =LEN(A1)-LEN(SUBSTITUTE(A1,",","")+LEN(A1)>0 -- Regards Frank Kabel Frankfurt, Germany Timothy Wright wrote: > I have some cells with note type data: > > For ex: > > ax,3b7,22,xy,zz, > > Is there a way to count every item between the commas? > > Thanks. On Wed, 19 May 2004 20:13:53 +0200, "Frank Kabel" <frank.kabel@freenet.de> wrote: >Hi >try >=LEN(A1)-LEN(SUBSTITUTE(A1,&q...

best way to deserialize xml
I'm at a quandry as to what's the best/fastest way to deserialize an incoming xml stream? I ran a small test on xmlserializer and on a small file it performed 10 times worse then when I used the XMLDocument and deserialized the values myself. Is the xmlserializer really that slow? I'm expecting a file about 1 MB in length and if it's that slow on a 2 K file then I can't phantom how it's going to behave on a 1 MB file. Should I just skip the xmlserializer and XML Dom and go straight to xmlreader and do the deserialization myself? This has been discussed a few times...

Is there a way to Sort data by color in Excel?
I am trying to sort data by color, is that possible? see http://www.xldynamic.com/source/xld.ColourCounter.html#sorting -- HTH Bob Phillips "SRTUC" <SRTUC@discussions.microsoft.com> wrote in message news:B5121232-FB1D-46AF-94F8-04B7EAA2D90C@microsoft.com... > I am trying to sort data by color, is that possible? You're gonna need a User defined function to get the value of a color. See Chip Pearson's site for a nice version: http://www.cpearson.com/excel/colors.htm Look for: Returning The ColorIndex Of A Cell Then you could use that UDF in a separate colu...

Is there a convenient way to save data
Is there a convenient way to save dialog data so it remembers the users last selection? I have a dialog based application that only has three user variables all of which are ints. What is the best way to make theses variables persistent for a given user on a given machine? Thanks "Jeff B" <jb@nospan.net> wrote in message news:fpDif.10451$Ay6.2336@fe13.lga... > > Is there a convenient way to save dialog data so it remembers the users last > selection? I have a dialog based application that only has three user > variables all of which are ints. What is the best w...

Fastest way to load an xml file into an array in .net
Hi All VS 2005 SP1 (C#, VB.NET) I need the absolut fastest way of loading an xml-file of about 700KB into an array. I need to load about 1500 of those xml-Files into arrays on startup of my application. Therefore it needs to be as fast as possible. Please also tell me if the xml structure can be improved. the xml looks like: y: Year m: Month h: Hour value <data> <v y="1998" m="1" h="1">0.227777109151971</v> <v y="1998" m="1" h="2">0.206573903224972</v> <v y="1998" m="1"...

Is there a way
G'Day, Is there a way to turn off NDR's in Ex5.5 SP4 on an NT server running SP6a ? Also, can the ability for the server to receive TELNET connections be turned off? My server isn't an open relay, but, I know it's being used to SPAM using DNR's and I can see TELNET connections in the log files.... Thank You, Admin_In_Syd i know that u can turn off the ndrs to be sent from exchange at the standard poliy setting, thats how i configured it at exchange 2000 .... there must be also a setting at exch 5.5, actually i dont remeber it .... "Steve@Home" <sprob@...