473,388 Members | 1,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Encoding string for xml

Hi all

I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?

Thanks
Kev
May 31 '06 #1
9 21048
This is off the cuff, so pardon any errors. I am sure it can be improved and
may have some bugs (;->). As it uses a switch (state machine), it should be
faster than a dual loop (character array and array of chars to look for).
The &#{number}; should be fine as escaped XML.

public string XmlEncode(string nonXmlText)
{
StringBuilder builder = new StringBuilder();
chars[] originalChars = nonXmlText.ToCharArray();

for(int i=0;i<originalChars.Length;i++)
{
switch((byte)originalChars[i])
{
case 34:
case 38:
case 39:
case 60:
case 61:
case 62:
builder.Append("&#");
builder.Append(originalChars(i);
builder.Append(";");
break;
default:
builder.Append(originalChars[i]);
break;
}
}

return builder.ToString();
}

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
"Mantorok" <ma******@mantorok.com> wrote in message
news:e5**********@newsfeed.th.ifl.net...
Hi all

I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?

Thanks
Kev

May 31 '06 #2
maybe the class XmlConvert does what you need

--
"Mantorok" <ma******@mantorok.com> schrieb im Newsbeitrag
news:e5**********@newsfeed.th.ifl.net...
Hi all

I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?

Thanks
Kev

May 31 '06 #3
Mantorok <ma******@mantorok.com> wrote:
I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?


Assuming that you specify the character set of the XML document
appropriately (eg to UTF-8) you shouldn't need any special handling.
You only really need to encode apostrophes, quotes, ampersands and
angle brackets (IIRC).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '06 #4
Can you provide an example or material that I can read on how this encoding
is done? I need to create an XML file with many PnP IDs listed within it.
Those IDs will have many "&" characters within the strings. I need to know
how to read them correctly from within an XML file that contains them.

Example:
<PnPIDs>
<ThisPnPID>PCI\VEN_14E4&DEV_167D&SUBSYS_0940103C&R EV_11\4&33E<\ThisPnPID>

This line will be manually copied to an XML file into the <ThisPnPID>
section. I will need to programatically read the string correctly.
--
-----------
Thanks,
Steve
"Jon Skeet [C# MVP]" wrote:
Mantorok <ma******@mantorok.comwrote:
I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?

Assuming that you specify the character set of the XML document
appropriately (eg to UTF-8) you shouldn't need any special handling.
You only really need to encode apostrophes, quotes, ampersands and
angle brackets (IIRC).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '06 #5
Oh, and I am using Visual Studio 2005 and .NET Frameworks 2.0.
--
-----------
Thanks,
Steve
"Jon Skeet [C# MVP]" wrote:
Mantorok <ma******@mantorok.comwrote:
I have a plain text string, sometimes the string will contain special
characters, how can I encode this string in xml format?

Assuming that you specify the character set of the XML document
appropriately (eg to UTF-8) you shouldn't need any special handling.
You only really need to encode apostrophes, quotes, ampersands and
angle brackets (IIRC).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '06 #6
SteveT <St****@newsgroups.nospamwrote:
Can you provide an example or material that I can read on how this encoding
is done? I need to create an XML file with many PnP IDs listed within it.
Those IDs will have many "&" characters within the strings. I need to know
how to read them correctly from within an XML file that contains them.

Example:
<PnPIDs>
<ThisPnPID>PCI\VEN_14E4&DEV_167D&SUBSYS_0940103C&R EV_11\4&33E<\ThisPnPID>

This line will be manually copied to an XML file into the <ThisPnPID>
section. I will need to programatically read the string correctly.
That's not valid XML. Processing invalid XML and turning into valid XML
is certainly doable, but there will always be a certain amount of
guesswork.

Why not make whatever creates the pseudo-XML file do the job properly?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '06 #7
Jon,

My knowledge of XML is limited. I'm learning but still a novice with it.
Are you suggesting that rather than manually creating this file I instead
programmatically create it and fix the PnP string that is inserted into the
XML file before hand with the correct encoding? If so, can you show me how
the encoding is done (to encode and de-encode :) )?
--
-----------
Thanks,
Steve
"Jon Skeet [C# MVP]" wrote:
SteveT <St****@newsgroups.nospamwrote:
Can you provide an example or material that I can read on how this encoding
is done? I need to create an XML file with many PnP IDs listed within it.
Those IDs will have many "&" characters within the strings. I need to know
how to read them correctly from within an XML file that contains them.

Example:
<PnPIDs>
<ThisPnPID>PCI\VEN_14E4&DEV_167D&SUBSYS_0940103C&R EV_11\4&33E<\ThisPnPID>

This line will be manually copied to an XML file into the <ThisPnPID>
section. I will need to programatically read the string correctly.

That's not valid XML. Processing invalid XML and turning into valid XML
is certainly doable, but there will always be a certain amount of
guesswork.

Why not make whatever creates the pseudo-XML file do the job properly?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '06 #8
SteveT <St****@newsgroups.nospamwrote:
My knowledge of XML is limited. I'm learning but still a novice with it.
Are you suggesting that rather than manually creating this file I instead
programmatically create it and fix the PnP string that is inserted into the
XML file before hand with the correct encoding? If so, can you show me how
the encoding is done (to encode and de-encode :) )?
Are you using .NET to create the file to start with? If so, I suggest
you either create the document in memory and then write it out (create
an XmlDocument) or stream it out with XmlWriter. In both cases, the
encoding of things like & will happen automatically.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '06 #9
I played around with XmlReader and XmlWriter with some sample code provided
by VS2005. I was successfully able to read and write the following XML file.
Thanks. This is what I was hoping to learn.

<?xml version="1.0" encoding="utf-8" ?>
- <pnpDevices>
<pnpid>PCI\VEN_1034&DEV_1000</pnpid>
</pnpDevices>
--
-----------
Thanks,
Steve
"Jon Skeet [C# MVP]" wrote:
SteveT <St****@newsgroups.nospamwrote:
My knowledge of XML is limited. I'm learning but still a novice with it.
Are you suggesting that rather than manually creating this file I instead
programmatically create it and fix the PnP string that is inserted into the
XML file before hand with the correct encoding? If so, can you show me how
the encoding is done (to encode and de-encode :) )?

Are you using .NET to create the file to start with? If so, I suggest
you either create the document in memory and then write it out (create
an XmlDocument) or stream it out with XmlWriter. In both cases, the
encoding of things like & will happen automatically.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 8 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Christopher H. Laco | last post by:
Long story longer. I need to get web user input into a backend system that a) only grocks single byte encoding, b) expectes the data transer to be 1 bytes = 1 character, and c) uses the HP Roman-6...
2
by: Eric Cadwell | last post by:
We are encoding strings using XMLElement: private string XMLEncode(string val) { if(val.Length == 0) return string.Empty; XmlElement element = xmldoc.CreateElement("E"); element.InnerText =...
8
by: Demon News | last post by:
I'm trying to do a transform (Using XmlTransform class in c#) and in the Transform I'm specifying the the output xsl below: <xsl:output method="xml" encoding="UTF-8" indent="no"/> the...
12
by: Tamir Khason | last post by:
I have Windows Form application recieved data from clipboard and convert its encoding based on some ruls. So doing following: //from source to multiple targets System.Text.Encoding targ1 =...
4
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
2
by: lprisr | last post by:
Hi, I have double byte characters in the content that I am returning using Web Services. However, the encoding in the xml file returned by Web Services is utf-8 and I am unable to read the...
4
by: Bob | last post by:
Hi Need to produce a Doc with no encoding info. Is there anyway of doing this? Thanks Bob i.e. <?xml version=\"1.0\" ?>
4
by: Terry Olsen | last post by:
I use the following code to create an XML string: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim tw As New StringWriter Dim xml...
8
by: Erwin Moller | last post by:
Hi group, I could use a bit of guidance on the following matter. I am starting a new project now and must make some decisions regarding encoding. Environment: PHP4.3, Postgres7.4.3 I must...
0
by: deloford | last post by:
Hi This is going to be a question for anyone who is an expert in C# Text Encoding. My situation is this: I have a Sybase database which is firing back ISO-8559 encoded strings. I am unable to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.