c# - How to work with an XmlWriter inside another? -


i'm trying solve similar problem described @ how check name of element writeendelement

i wrote code inner , outer xmlwriter. idea closing inner xmlwriter close tags left hanging not-perfectly-reliable third party library.

var sb = new stringwriter();  using (var xml = xmlwriter.create(sb, new xmlwritersettings(){indent = true})) {     xml.writestartelement("root");      using (var inner = xmlwriter.create(xml))     {         inner.writestartelement("payload1");          // simulate thirdpartylibrary.serialise(results, inner) leaving tag open         inner.writestartelement("third-party-stuff");     }      xml.writestartelement("payload2"); }  sb.tostring().dump(); 

i expect produce

<root>   <payload1>     <third-party-stuff />   </payload1>   <payload2 /> </root> 

but instead run-time error @ line should write <payload2>

invalidoperationexception
writer closed or in error state.

why error? didn't expect closing inner xmlwriter close outer one.

using variable names in code sample closing xmlwriter inner release (outer) xmlwriter xml. expected behaviour according this msdn entry disposing xmlwriter "releases resources used current instance of xmlwriter class" ie release "outer" xmlwriter created with.

such code generate code analysis error ca2202. resolution such situations described in following link.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -