Encoding string for xml

M

Mantorok

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
 
C

Cowboy \(Gregory A. Beamer\)

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)
{
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);
break;
}
}

return builder.ToString();
}

--
Gregory A. Beamer

*************************************************
Think Outside the Box!
*************************************************
 
J

Jon Skeet [C# MVP]

Mantorok said:
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).
 
G

Guest

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&REV_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.
 
J

Jon Skeet [C# MVP]

SteveT said:
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&REV_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?
 
G

Guest

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 :) )?
 
J

Jon Skeet [C# MVP]

SteveT said:
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.
 
G

Guest

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top