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?

8
  • What's the exact problem you're having? Commented Dec 26, 2012 at 5:47
  • 2
    Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. Commented Dec 26, 2012 at 5:48
  • Here is an example: I have a node in file like this. "<node>this < that</node>" this will produce error because the node contains < Commented Dec 26, 2012 at 5:50
  • 1
    That's illegal XML. It must be &lt; Commented Dec 26, 2012 at 5:51
  • you should put the data in CDATA stackoverflow.com/a/2784294/158297 Commented Dec 26, 2012 at 5:51

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.