How to load XML with special characters using XDoc

2019-09-15 05:09发布

问题:

**This is for silverlight application.

I have a stream with xml data, which after loading massage the data and generate final file. I start by load using XDocument.Load(stream). One of the file was failing to load and after some research I foung out that one of the element has a value of 'First & Second' and load fails on hitting &.

Is it possible to load XML with special characters? ( I am sure the answer is no, but worth asking).

Is it possible to preprocess the XML (without performance hit) and change the special characters to someother characters and after the process put it back to normal value.

回答1:

In XML, & should be & otherwise it's not valid XML. If you don't have control over the XML files, load them as text, replace & with & in memory (but be careful not to replace the & that are part of entities like existing & > or < !) and load them as XML again.