Omitting XML processing instruction when serializing an object?

The following link will take you to a post where someone has a method of supressing the processing instruction by using an XmlWriter and getting into an 'Element' state rather than a 'Start' state. This causes the processing instruction to not be written.

The following link will take you to a post where someone has a method of supressing the processing instruction by using an XmlWriter and getting into an 'Element' state rather than a 'Start' state. This causes the processing instruction to not be written. Suppress Processing Instruction If you pass an XmlWriter to the serializer, it will only emit a processing instruction if the XmlWriter's state is 'Start' (i.e.

, has not had anything written to it yet). For example: // Assume we have a type named 'MyType' and a variable of this type named 'myObject' System.Text. StringBuilder output = new System.Text.StringBuilder(); System.IO.

StringWriter internalWriter = new System.IO. StringWriter(output); System.Xml. XmlWriter writer = new System.Xml.

XmlTextWriter(internalWriter); System.Xml.Serialization. XmlSerializer serializer = new System.Xml.Serialization. XmlSerializer(typeof(MyType)); writer.

WriteStartElement("MyContainingElement"); serializer. Serialize(writer, myObject); writer.WriteEndElement(); In this case, the writer will be in a state of 'Element' (inside an element) so no processing instruction will be written. One you finish writing the XML, you can extract the text from the underlying stream and process it to your heart's content.

Although at least they will be known, so that's good... – Blair Conrad Oct 2 '08 at 23:40 1 I played with this and replaced WriteStartElement with WriteRaw("") and got rid of the WriteEndElement - then all I needed to do to get good output was TrimStart() the BOM off. Cool! – Blair Conrad Oct 3 '08 at 0:27.

In 2.0, you would use XmLWriterSettings. OmitXmlDeclaration, and serialize to an XmlWriter - however I don't think this exists in 1.1; so not entirely useful - but just one more "consider upgrading" thing... and yes, I realise it isn't always possible.

I made a small correction XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings. OmitXmlDeclaration = true; using ( XmlWriter stringWriter = XmlWriter. Create(builder, settings) ) { serializer.

Serialize(stringWriter, comments); return builder.ToString(); }.

Thanks for posting the correction :o) – Andrew Jan 29 '10 at 12:29.

Instead of using XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces. Add("", ""); ex.

1 stackoverflow. Com/questions/625927/… – NetSide Mar 9 '09 at 12:44.

If by "processing instruction" you mean the xml declaration, then you can avoid this by setting the OmitXmlDeclaration property of XmlWriterSettings. You'll need to serialize using an XmlWriter, to accomplish this. XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); StringBuilder builder = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings(); settings.

OmitXmlDeclaration = true; using ( XmlWriter stringWriter = new StringWriter(builder, settings) ) { serializer. Serialize(stringWriter, comments); return builder.ToString(); } But ah, this doesn't answer your question for 1.1. Well, for reference to others.

This works in . NET 1.1. (But you should still consider upgrading) XmlSerializer s1= new XmlSerializer(typeof(MyClass)); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns. Add( "", "" ); MyClass c= new MyClass(); c.

PropertyFromDerivedClass= "Hallo"; sw = new System.IO.StringWriter(); s1. Serialize(new XTWND(sw), c, ns); .... /// XmlTextWriterFormattedNoDeclaration /// helper class : eliminates the XML Documentation at the /// start of a XML doc. /// XTWFND = XmlTextWriterFormattedNoDeclaration public class XTWFND : System.Xml.

XmlTextWriter { public XTWFND(System.IO. TextWriter w) : base(w) { Formatting = System.Xml.Formatting. Indented; } public override void WriteStartDocument() { } }.

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