.comment-link {margin-left:.6em;}

Ben Cops

Monday, July 05, 2004

Save an XML Document to a stream without the XML declaration

This is really handy if you feel the encoding attribute of the XML declaration you're forced to accept from XMLDocument.Save() is causing you grief:
GotDotNet User Sample: XmlFragmentWriter for Xml Serialization

Basically it uses reflection to set a private field on an XMLTextWriter to fool the writer into thinking its already written the declaration. Naughty!


// get to the currentState field
Type xmlTextWriterType = typeof( System.Xml.XmlTextWriter );
FieldInfo currentStateFieldInfo = xmlTextWriterType.GetField(
"currentState", BindingFlags.Instance | BindingFlags.NonPublic );

// get the State enumeration
Type stateEnumType = xmlTextWriterType.GetNestedType( "State",
BindingFlags.NonPublic );

// set the currentState to Prolog written
currentStateFieldInfo.SetValue( this,
stateEnumType.GetField( "Prolog" ).GetValue( this ) );

27 Comments:

  • ceQ3QC The best blog you have!

    By Anonymous Anonymous, at 11:21 am  

  • i37BWz Wonderful blog.

    By Anonymous Anonymous, at 5:04 pm  

  • Hello all!

    By Anonymous Anonymous, at 6:02 pm  

  • Thanks to author.

    By Anonymous Anonymous, at 6:51 pm  

  • Hello all!

    By Anonymous Anonymous, at 7:37 pm  

  • Magnific!

    By Anonymous Anonymous, at 8:43 pm  

  • Magnific!

    By Anonymous Anonymous, at 10:20 am  

  • Thanks to author.

    By Anonymous Anonymous, at 2:54 pm  

  • actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

    By Anonymous Anonymous, at 3:56 pm  

  • Wonderful blog.

    By Anonymous Anonymous, at 5:03 pm  

  • actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

    By Anonymous Anonymous, at 5:52 pm  

  • 5I4zp8 write more, thanks.

    By Anonymous Anonymous, at 8:15 pm  

  • Wonderful blog.

    By Anonymous Anonymous, at 4:39 am  

  • actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

    By Anonymous Anonymous, at 6:27 am  

  • Hello all!

    By Anonymous Anonymous, at 7:03 am  

  • Thanks to author.

    By Anonymous Anonymous, at 7:35 am  

  • Hello all!

    By Anonymous Anonymous, at 8:03 am  

  • actually, that's brilliant. Thank you. I'm going to pass that on to a couple of people.

    By Anonymous Anonymous, at 8:36 am  

  • Good job!

    By Anonymous Anonymous, at 9:05 am  

  • Nice Article.

    By Anonymous Anonymous, at 9:46 am  

  • Magnific!

    By Anonymous Anonymous, at 10:30 am  

  • Please write anything else!

    By Anonymous Anonymous, at 11:04 am  

  • Thanks to author.

    By Anonymous Anonymous, at 11:35 am  

  • Calvin, we will not have an anatomically correct snowman!

    By Anonymous Anonymous, at 12:07 pm  

  • Give me ambiguity or give me something else.

    By Anonymous Anonymous, at 12:44 pm  

  • Magnific!

    By Anonymous Anonymous, at 1:22 pm  

  • Give me ambiguity or give me something else.

    By Anonymous Anonymous, at 1:48 pm  

Post a Comment

<< Home