How to tell if a string is xml?

后端 未结 7 1725
北荒
北荒 2021-01-11 10:44

We have a string field which can contain XML or plain text. The XML contains no header, and no root element, i.e. is not well formed.

We need t

7条回答
  •  遥遥无期
    2021-01-11 11:11

    If your goal is reliability then the best option is to use XmlDocument.LoadXml to determine if it's valid XML or not. A full parse of the data may be expensive but it's the only way to reliably tell if it's valid XML or not. Otherwise any character you don't examine in the buffer could cause the data to be illegal XML.

提交回复
热议问题