0

I have an object to be serialized using XMLSerializer.serialize(). I have successfully serialize the objects to XML string then write it to file and the file can be read successfully using XmlDocument.load().

But now, a value in object's attribute contains <. The serialize function serializes the object successfully but XMLDocument.load() failed reading the file because the < is found with a wrong format. Is there any function to replace any attribute value in object which contains < or > to &lt; or &gt; without replacing it one by one? Or there is another way to do this?

John Saunders
  • 159,224
  • 26
  • 237
  • 393
Darwin Gautalius
  • 317
  • 4
  • 13

2 Answers2

1

XML file generated by XmlSerializer should be valid XML file, or you found a bug in .NET framework (that's very unlikely to happend as this class/method is so popular)

So, please check if there's any chance that the XML file generated has been modified by something else. And if so, you might want to fix the modifier instead of correcting the XML file manually.

Reinhard
  • 90
  • 12
0

Is it possible to use Regex.Replace() to replace special character before it is passed down to be serialize?

Another way would be to use try-catch, and then use a function to replace it when the error is caught. Source: here

Hearty
  • 543
  • 1
  • 5
  • 15