views:

121

answers:

3

Are there methods for encoding and decoding *XML* in .NET? I can't seem to find them and am wondering why they aren't there and what to use instead?

I need to encode an XML document and pass it through to a string parameter on a web service. It then needs to be decoded at the other end.

A: 

Actually with the nice objects in System.Xml.Linq you need not worry.

What I mean is you will not get a runtime exception if you run this code.

var element = new XElement("Name", "<Node />");

The value of the element will be a text node with &lt;Node /&gt;.

ChaosPandion
@ChaosPandion: Can you please be more specific?
Alex Angas
@Alex: LINQ to XML and all the other XML APIs in .NET understand when strings need to be escaped and when they do not. In the example above, the value of the element will be a text node with `<Node />`.
John Saunders
@John - To put it more eloquently. :)
ChaosPandion
A: 

If you are referring to encoding/decoding of XML names, there is XmlConvert.EncodeName and DecodeName.

Or are you talking about specifying the encoding/decoding of the whole XML document using XmlDeclaration or XDeclaration? (I thought this took care of encoding for us)

Si
A: 

If you're passing XML as a string parameter (very bad web service design, BTW), then you don't have to do anything. It's up to the web service to do any encoding that may be necessary. Just use XDocument.ToString() or whatever and pass the result to the web service.

John Saunders
@John Saunders: You might want to check out [Passing XML as a parameter to a web service](http://stackoverflow.com/questions/2597615/passing-xml-as-a-parameter-to-a-web-service)
Alex Angas