473,795 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I convert a string to XML string

I need to convert a text string ("Dewey & Cheatham & Howe") to an XML encoded string ("Dewey & Cheatham & Howe"). I am not building an XML document, I am just trying to convert a single string. I have looked at the System.Xml namespace, but I can't quite find what I need. XmlTextWriter.W riteString() does the type of encoding I need, but it writes the string. I just want it to return the encoded string.

Can someone tell me the class and method I need?

Thanks.

*************** *************** *************** *************** **********
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 11 '05 #1
4 60001
jo*******@genpt .com wrote:
I need to convert a text string ("Dewey & Cheatham & Howe") to an XML encoded string ("Dewey & Cheatham & Howe"). I am not building an XML document, I am just trying to convert a single string. I have looked at the System.Xml namespace, but I can't quite find what I need. XmlTextWriter.W riteString() does the type of encoding I need, but it writes the string. I just want it to return the encoded string.

Can someone tell me the class and method I need?


Well, I'm not aware of any available method unfortunately. But in fact
all you need is to replace & and < chars. They are only chars forbidden
in XML in text, so double String.Replace or some regexp would suffice
your needs:

s.Replace("&", "&amp;").Replac e("<", "&lt;")

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
"Joe Rattz" <jo*******@genp t.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
: I need to convert a text string ("Dewey & Cheatham & Howe") to an XML
: encoded string ("Dewey &amp; Cheatham &amp; Howe"). I am not building an
: XML document, I am just trying to convert a single string. I have looked
: at the System.Xml namespace, but I can't quite find what I need.
: XmlTextWriter.W riteString() does the type of encoding I need, but it
: writes the string. I just want it to return the encoded string.

This will do the trick:

public class XmlEncoder
{
private XmlDocument temp;

public XmlEncoder()
{
temp = new XmlDocument();
temp.LoadXml("< foo/>");
}
public string Encode(string input)
{
temp.DocumentEl ement.InnerText = input;
return temp.DocumentEl ement.InnerXml;
}
}

Bob Rossney
rb*@well.com
Nov 11 '05 #3
"Oleg Tkachenko" <oleg@NO_!SPAM! _PLEASEtkachenk o.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
: jo*******@genpt .com wrote:
:
: > I need to convert a text string ("Dewey & Cheatham & Howe") to an XML
encoded string ("Dewey &amp; Cheatham &amp; Howe"). I am not building an
XML document, I am just trying to convert a single string. I have looked at
the System.Xml namespace, but I can't quite find what I need.
XmlTextWriter.W riteString() does the type of encoding I need, but it writes
the string. I just want it to return the encoded string.
: >
: > Can someone tell me the class and method I need?
:
: Well, I'm not aware of any available method unfortunately. But in fact
: all you need is to replace & and < chars. They are only chars forbidden
: in XML in text, so double String.Replace or some regexp would suffice
: your needs:
:
: s.Replace("&", "&amp;").Replac e("<", "&lt;")

The problem is a little more complicated than this, because it depends on
how the string is to be used.

For text content of an element, yes, just escaping ampersands and less-than
signs will do. For attribute content, escaping single and double quotes may
be necessary. For text in a CDATA section, a greater-than sign must be
escaped if it's preceded by two right brackets (otherwise the "]]>" will be
interpreted as the end of the CDATA section). See
http://www.w3.org/TR/REC-xml#syntax.

Why would anyone want to escape text for XML outside of the context of an
XML document, anyway? This smells like someone solving the wrong problem to
me.

Bob Rossney
rb*@well.com

Nov 11 '05 #4
Sniff ... sniff ... sniff. 8-)


*************** *************** *************** *************** **********
Sent via Fuzzy Software @ http://www.fuzzysoftware.com/
Comprehensive, categorised, searchable collection of links to ASP & ASP.NET resources...
Nov 11 '05 #5

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

Similar topics

2
8945
by: Joel Moore | last post by:
Maybe I'm just easily baffled after an all-nighter but I can't seem to figure out how to represent a BitArray as a hexadecimal string. For example: Dim outputBank As New BitArray(8) outputBank(0) = True outputBank(1) = False outputBank(2) = False
4
3637
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
4
9772
by: aevans1108 | last post by:
expanding this message to microsoft.public.dotnet.xml Greetings Please direct me to the right group if this is an inappropriate place to post this question. Thanks. I want to format a numeric value according to an arbitrary regular expression.
3
10295
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
7
29247
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
6
1407
by: patang | last post by:
Could someone please tell me where am I supposed to put this code. Actually my project has two forms. I created a new module and have put the following code sent by someone. All the function declaration statments (first lines) e.g. Public Function ConvertCurrencyToEnglish(ByVal MyNumber As Double) As String Private Function ConvertHundreds(ByVal MyNumber As String) As String etc.
3
13861
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what exactly I should do. Please give me some sample code if you could. Thanks a lot Regards, Gary
4
5078
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
9
17580
by: Marco Nef | last post by:
Hi there I'm looking for a template class that converts the template argument to a string, so something like the following should work: Convert<float>::Get() == "float"; Convert<3>::Get() == "3"; Convert<HelloWorld>::Get() == "HelloWorld"; The reason I need this is that in our design every class of a certain
0
10785
Debadatta Mishra
by: Debadatta Mishra | last post by:
Introduction In this article I will provide you an approach to manipulate an image file. This article gives you an insight into some tricks in java so that you can conceal sensitive information inside an image, hide your complete image as text ,search for a particular image inside a directory, minimize the size of the image. However this is not a new concept, there is a concept called Steganography which enables to conceal your secret...
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10216
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.