• Answers
  • Web

Ordinary People Discovering Extraordinary Abilities

Not So Smart Builder

String to xml format string in C#

How do I convert a string to valid xml string? I'd like to convert all the characters so my xml will be valid. I need a C# example.


Share Send to a friend Watch Report
 

Best Answer

 
8 helpful answers

I think this should do it:

public static string EscapeXml(string s)
{
  XmlDocument doc = new XmlDocument();
  XmlElement element = doc.CreateElement("temp");
  element.InnerText = s;
  return element.InnerXml;
}

If you're going to do a lot of escaping, you might want to create the XmlDocument and XmlElement once.

 

Helpful?(6)
Rated as Best Answer

 

All Answers
Order by

 
207 helpful answers

They're coming to take me away, Ha-haaa!

Advanced .NET Debugging Blog

My personal blog

Actually, depending on the escaping you wish to perform, you can use two functions that are available in .NET which will make sure your text is valid within XML.

The first, is to use the HttpUtility.HtmlEncode. Since most of the HTML encoding ("<", ">", "&" and the rest) also needs to be encoded in XML, this function will make sure the text you are entering into an XML Text node will be just fine. 

If you need to encode a text that will be used as the name of an element (not its content) you can use XmlConvert.EncodeLocalName which will turn every unsupported character to a special representation that can also contain unicode characters. 

Posted 2006-06-19T07:37:44Z
 

great post and great comments

thank you everyone very much

 

 

comment posted by canon xs

 

 System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(@"<?xml version=""1.0""?><NextUrl>"+ nextUrl +"</NextUrl>");
            System.Xml.XmlNode element = doc.SelectSingleNode("NextUrl");

 

where nextUrl string type consist value

 


        Reviews 1-3 of 17                    :
                                                        <a href="#reviews" class="yls-gl-pagon">1</a>
                                                <a href="http://local.yahoo.com/details?id=20634987&csz=Encino%2C+CA&sortreviews=1&from=4#reviews">2</a>
                                                <a href="http://local.yahoo.com/details?id=20634987&csz=Encino%2C+CA&sortreviews=1&from=7#reviews">3</a>
                                                <a href="http://local.yahoo.com/details?id=20634987&csz=Encino%2C+CA&sortreviews=1&from=10#reviews">4</a>
                                                <a href="http://local.yahoo.com/details?id=20634987&csz=Encino%2C+CA&sortreviews=1&from=13#reviews">5</a>
                                                <a href="http://local.yahoo.com/details?id=20634987&csz=Encino%2C+CA&sortreviews=1&from=16#reviews">6</a>
                                                        <a href="http://local.yahoo.com/details?id=20634987&csz=Encino%2C+CA&sortreviews=1&from=4#reviews" class="yls-gl-pagarrow">Next <span>&#187;

how can i get value

XElement element = new XElement(pageSection);
var value = from tag in element.Descendents("a")
where tag.Value equals "Next"
select tag.Attribute("href").Value

 

using n how can i use

 

 

 

 

Sign in to participate

Got an answer for dudushmaya? Would you like to comment on the posted answers, or vote for the one which you think is the best?

Sign up for a free account, or sign in (if you're already a member).

Explore Related Questions

Other people asked questions on similar topics, check out the answers they received:


Q:

Formatting a C# string to a proper XML format

I am looking for a way to take a C# string and format it so that it is in proper XML format. EX: "<quiz><question><text>This is ...
Submitted by Questions   3 years ago.
  • viewed 3141 times
Last answer posted 2 years ago by robertc


Q:

What is c#

what is c#
Submitted by pradeepbaloni   2 years ago.
  • viewed 589 times
Last answer posted 2 years ago by Eran


Q:

How can i convert the integer into string on linq ...

how can i convert the integer into string on linq database by using c#
Submitted by muneesh   2 years ago.
  • viewed 2769 times
Last answer posted 3 months ago by jorge



» More...

Explore Related Posts in Forums

C-C-C-C-Combo

C-C-C-C-Combo C-C-C-C-C-ComboC-C-C-C-C-C-C-Combo! This post... -C-C-C-C-Combo I STILL BELIEVE IN BLUEQuote: C-C-C-C-C-...

C-c-c-c-cold

... It all counts in your favor... NixieHi Gypsy, I would GLADLY trade the hot flashes for feeling c -c-c-cold... I am well past menopause, but I still get these horrid hot flashes... (and heat I do notice that after I lost 80 pounds, I now feel colder during the fall and winter months. I also think it has something...

6/11 - C C Penny pitch. C C Sox win. C C Sabathia die like a dog.

The Sox lead the Yankees 7-0 on the season for the first time since 1912, when: the Titanic sank; Fenway Park opened; France... legalized sodomy ; and some German synthesized MDMA (aka ecstasy). So let's sodomize these Titanic-salaried bastards... Lead pipe these sumbitches. This post has been edited by JohnnyTheBone : Jun 11 2009, 09:14 AMCan we title this: Battle of...
» More...
Powered by
Feed - Subscribe to changes to this Q&A Blog
ADVERTISEMENT
  • Answers
  • Web
Copyright © 2006-2009, Yedda Inc. and respective copyright owners · CC License