Sebastian Müller
Sebastian Müller

Reputation: 5589

<, > in xml document

When I try to insert signs like < or > in an InnerText-part of a xml-tag then after accessing the xml by xmlDocument.InnerXml these signs are replaced by their html-code like &lt or &gt, can someone explain this to me and perhaps give me a solution for this problem?

with kind regards

Sebastian

Upvotes: 0

Views: 277

Answers (4)

Bevan
Bevan

Reputation: 44327

By definition (see the spec) in an XML file, < is used to open an element. Similarly, > is used to close an element.

To allow for these (and a few other characters) to appear in the XML file, they get encoded as entities.

You should find this is all transparently handled for you - if you put a < into the content of an element, it will be stored as &lt; and then translated back when you get the content back out again.

If this is getting in your way because you're trying to add extra elements into your output file, you should look at the various APIs for creating elements directly -- writing text is going to fail.

Upvotes: 3

DarkwingDuck
DarkwingDuck

Reputation: 2706

It depends what you want to appear in the inner text. If you are actually trying to insert a new element, you are better off adding a new node instead to the Nodes collection (type = element). If you actually want text that renders as < and > then this is already doing exactly what you need.

If you can explain the final result you are aiming for with this operation, we can tell you exactly what you need to do.

Upvotes: 0

Filip Frącz
Filip Frącz

Reputation: 5947

You could also try enclosing your text in the CDATA section.

Upvotes: 4

Scott Muc
Scott Muc

Reputation: 2883

Those symbols have to be escaped because angle brackets are used as document structures. To deal with the issue, just make sure you're decoding the text properly.

Upvotes: 5

Related Questions