0

I have an xml. The value is from user input.

<Item id="36">
    <name>Electrical and lighting - Exterior lighting</name>
    <note><123comments></note>
    <info id="code">&*&^<samp&*></info>
</Item>

when saving the xml, I'm having an error "XML parsing: line 14, character 1868, end tag does not match start tag"

How can I can convert the value inside the node, without replacing the other < or >?

  • in c# or js
shololauy
  • 13
  • 1
  • 1
  • 3
  • 1
    The snippet is not valid XML. – MrTux Jul 15 '20 at 02:38
  • Your XML is invalid. `<123comments>` is not valid content for an element. – Ken White Jul 15 '20 at 02:42
  • 1
    @KenWhite and MrTux: Technically in XML terms, it's not [*well-formed*](https://stackoverflow.com/a/25830482/290085). – kjhughes Jul 15 '20 at 02:56
  • thanks for your answers, is there a way on how to correct the value? but it will still save the same way, do I have to look for each tag? – shololauy Jul 15 '20 at 13:07
  • XML must be well-formed, or it's not XML and it's not able to be used by XML libraries or tools. I show in my answer one way to make your XML be well-formed; there are others. If you'd like another way, you'll have to specify what you do not like about the way I showed you. For example, you might say that you want `123comments` to be an opening tag; then, I'd say it must be closed and it must not start with a number. And so on for any other variation you might ask about. But you'll have to be specific about the variation. – kjhughes Jul 15 '20 at 13:18
  • Reading the link I gave you about [***well-formed***](https://stackoverflow.com/a/25830482/290085) should help you learn about this concept in general so you'll be able to evaluate your options better. – kjhughes Jul 15 '20 at 13:20

2 Answers2

0

I hope this works for you.

The problem is inside the info and note tags, the value can't have < or >, so you should try to encode the value of that tag.

  • do you have a sample on how to do that? do I need to look for each tag to check the values? – shololauy Jul 15 '20 at 13:05
  • Sure, the tags that you should encode are: note and info tag. Because the value the have are invalid for xml. Too, in order to solved that. Encode does values. I personally use Base64 encoding. In c# you can achieve that in the example of these old answer: https://stackoverflow.com/a/11743162/10631412 – Carlos Acosta Jul 17 '20 at 15:38
0

Here is your XML corrected to be well-formed:

<Item id="36">
    <name>Electrical and lighting - Exterior lighting</name>
    <note>&lt;123comments></note>
    <info id="code">&amp;*&amp;^&lt;samp&amp;*></info>
</Item>

Notes:

  • < must be escaped as &lt; unless it's beginning a tag.
  • & must be escaped as &amp; unless it's beginning an entity.

For further details on which characters must be escaped in XML, see Simplified XML Escaping.

kjhughes
  • 106,133
  • 27
  • 181
  • 240