简体   繁体   中英

Validate or parse a string for XML?

I have a Description textbox on the page. When enter the data in that and submit the page. I will pass that string to XML tag in the XML file.

If user enter any invalid characters in textbox which are not allowed for xml. How to remove or parse them from string? I need to validate string for XML data.

If you're using the XmlDocument or XDocument classes to build the XML then you don't need to worry as they'll do the encoding for you.

Otherwise, if you generating the XML by hand you can use the SecurityElement.Escape method to encode invalid XML characters

That depends on how you are creating the XML. If you are assembling the XML string yourself, there are A LOT of things you should do and take into consideration.

Thus, you should not be doing that (assembling the string yourself).

.NET provides you with abstraction layers so you don't have to deal with that. Example: XDocument

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM