tags:

views:

36

answers:

1

Try this:

var doc1 = XDocument.Load(@"C:\any.xml", LoadOptions.SetLineInfo);
var doc2 = new XDocument(doc1);

doc2 no longer has any line number information. Digging in with Reflector, I can see that when the nodes are cloned from doc1 to doc2 this does not preserve the annotations on the XObject base type, which includes the line number information accessible via IXmlLineInfo. Nor does it retain the BaseUri, which I also need.

Any ideas how I can clone the document while preserving line numbers? I found this but it doesn't preserve BaseUri and is a bit of a hack.

A: 

You could save it into a memory stream and then reload it into a new instance of XDocument.

Jeff Yates
That would still lose the original BaseUri and line number information, I'm afraid.
Matt Howells
Hmm, that's a shame, but yes, of course it would. Could you get away with wrapping the new instance with something that can hold the original BaseUri?
Jeff Yates