XmlSerializer Producing XML With No Namespace Prefix?

First of all, if the consumer of your string were processing XML, then they wouldn't care about the prefix, since it doesn't matter (to XML). Perhaps they don't understand XML, and think they're processing a string (which might need to have the string "ps:" on every element).

First of all, if the consumer of your string were processing XML, then they wouldn't care about the prefix, since it doesn't matter (to XML). Perhaps they don't understand XML, and think they're processing a string (which might need to have the string "ps:" on every element). Second of all, you should change your code a bit: XmlSerializer xs = new XmlSerializer(pObject.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { XmlWriterSettings settings = new XmlWriterSettings() { Encoding = Encoding.

UTF8 }; using (XmlWriter writer = XmlWriter. Create(memoryStream, settings)) { xs. Serialize(writer, pObject); } return Encoding.

UTF8. GetString(memoryStream.ToArray()); } This will properly dispose of the stream and XmlWriter if an exception is thrown, stops using the deprecated XmlTextWriter class, and yet still returns a string containing XML written for UTF-8. Finally, to control the prefix, see "How to: Qualify XML Element and XML Attribute Names": XmlSerializerNamespaces myNamespaces = new XmlSerializerNamespaces(); myNamespaces.

Add("ps", "www.ladieda.com"); XmlSerializer xs = new XmlSerializer(pObject.GetType()); using (MemoryStream memoryStream = new MemoryStream()) { XmlWriterSettings settings = new XmlWriterSettings() { Encoding = Encoding. UTF8 }; using (XmlWriter writer = XmlWriter. Create(memoryStream, settings)) { xs.

Serialize(writer, pObject, myNamespaces); } return Encoding. UTF8. GetString(memoryStream.ToArray()); }.

2 +1 excellent answer - great explanations and good, easy-to-follow code samples. – marc_s May 14 '10 at 20:55 Great! This works out of the box!

Thanks mate. – Michel May 14 '10 at 21:01 Very weird: this code produces a (in the debugger) non visible character as first character of the xml string. It only pops up if you compare it with the same string without this character, or if you html.

Encode it, because the non-visible character does get encoded. It's only 1 char, and the workaround so far is to strip it off. – Michel May 26 '10 at 12:48 Interesting.

That's probably a UTF-8 Byte Order Mark (BOM). – John Saunders May 26 '10 at 13:22.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions