0

I am trying to convert string to xml with using of loadXML.

<name>alex</name> -> when string is well formatted for xml conversion, there is no issue.

<result> 5 < 3 </result> -> but when string has invalid characters for xml value, it throws an exception.

How can I convert string with invalid characters to XmlDocument? Is there any way to replace invalid characters with xml escape characters without touching tags in string?

Is there any way to change <result> 5 < 3 </result> to <result> 5 &lt; 3 </result> in string without touching tags ?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • 2
    That XML is perfectly valid. If it were `5 < 3` that would be a different matter. As for getting invalid XML to parse - I wouldn't expect you to have much luck. Most XML APIs expect you to start off with valid XML. I suggest you fix whatever is generating broken XML. – Jon Skeet Mar 18 '14 at 23:52
  • I am reading this string from another program that i have no control on. e.g '' So i can't do anything before reading string, I need to do something after reading string and before trying to convert to xml. – user3217062 Mar 19 '14 at 00:02
  • Do you know the schema of the expected XML? – Ismail Hawayel Mar 19 '14 at 00:18
  • No, I am sending sql query to external program and it returns string message with invalid characters. Schema is not constant. – user3217062 Mar 19 '14 at 00:26
  • Not sure if that question would be applicable to what you trying to do: http://stackoverflow.com/questions/8331119/escape-invalid-xml-characters-in-c-sharp – Ismail Hawayel Mar 19 '14 at 00:30
  • 3
    ` 5 < 3 ` is not XML. Don't try to convert not-XML to XML, reject it and tell whatever is sending it to send you XML. – Dour High Arch Mar 19 '14 at 00:35
  • thanks, but the solution you sent only worked if i have the 5 < 3 part of xml. I also have the starting tag and ending tag. It will also remove tags '<', '>' , '/' characters. After that it's not possible to convert that string to xml. – user3217062 Mar 19 '14 at 00:36
  • @DourHighArch, what i am asking is, is there any way change 5 < 3 to 5 < 3 in string without touching to tags? – user3217062 Mar 19 '14 at 00:47
  • 1
    If the data is not valid XML, then you cannot produce an XML Document from that data. Insist on valid XML, or else ignore such bad data entirely. – John Saunders Mar 19 '14 at 02:22
  • @user3217062: What we've been saying is that you're not going to be able to do that with the normal XML APIs, because you don't have valid XML. As John says, you should insist on decent XML. It's a very bad sign when you're given "not quite XML" to start with, as if the producers can't even get XML right (by using a good XML API) then goodness knows what else they're getting wrong. – Jon Skeet Mar 19 '14 at 06:45

1 Answers1

0

I think the better way to answer your problem is this one : make your own encode language. For example : "3 < 5" become : "3 inf 5".

In this case it works.

After, you just have to parse with something like :

string result = "3 inf 5";
result = result.replace("inf", "<");

Hope it helps you.

carndacier
  • 960
  • 15
  • 38