How to stop XMLReader by throwing an Invalid XML Character Exception

So I have XML:

<key>my tag</key><value>my tag value &#xB;and my invalid Character</Value>

and XMLReader:

 using (XmlReader reader = XmlReader.Create(new StringReader(xml))) { while (reader.Read()) { //do my thing } } 

I implemented the CleanInvalidCharacters method from here , but since "& #xB" is not yet encoded, it is not deleted.

The error is read by the reader. Read (); line with an exception:

the hexadecimal value 0x0B is an invalid character.

+5
source share
1 answer

The problem is that you do not have XML - you have a string that will probably look like XML, but, unfortunately, does not fit. Fortunately, you can tell XmlReader be softer:

 using (XmlReader reader = XmlReader.Create(new StringReader(xml), new XmlReaderSettings { CheckCharacters = false })) { while (reader.Read()) { //do my thing } } 

Note that you will still get XML, which when serialized can lead to further problems, so you may want to filter out the characters after that anyway when you read it.

+8
source

Source: https://habr.com/ru/post/1204638/


All Articles