Simple XML Question

Simple XML Question

倚栏听风 发布于 2021-11-27 字数 403 浏览 752 回复 5 原文

I am creating XML at runtime its schema is like


The problem is when I am trying to save some HTML contents to this Tag


How to resolve this issue

Thanks in advance

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。



需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。


蓝眼泪 2022-06-07 5 楼

The less than tag (<) must be escaped using &lt; and same for the > tag (&gt;)

你的他你的她 2022-06-07 4 楼

The HTML string needs to be properly escaped before you add it to the xml. If you are using .NET here are some ways to do it.

放手` 2022-06-07 3 楼

You can embed the HTML content in a CDATA section:

<Item><![CDATA[  <Content><strong>Hi</strong></Content> ]]></item>
蘸点软妹酱 2022-06-07 2 楼

I assume that you have a schema that permits an Item element to contain a Content element and that the Content element can only contain text or CDATA or similar. You have two options in that case.

Firstly, you could escape the html somehow. Either you could use a CDATA section as Fredrik suggested above. Alternatively, you could escape the bracketing as above. Both of these solutions would allow you to continue to treat the contents of Content as text. This lets you have a simple content model for your element.

Alternatively, you could extend your schema to allow xhtml elements as part of the Content element. I suggested a way to that here. Of course, if your content is html not xhtml this won't work.

Really, the choice comes to whether or not you want to be able to parse the embedded html as part of your xml or not. If you want it be text, escape it. If you want it to be parseable, extend your schema.

九公里浅绿 2022-06-07 1 楼

The correct answer is to not embed tags in XML. The XML should only define the data, the parser should put it in the right markup e.g. all Item->Contents in <strong></strong>.

The other solution is to escape the tags using XML escapes: &lt; and &gt;.