c# - Include XML CDATA in an element -
update: added more detail per request
i trying create xml configuration file application. file contains list of criteria search , replace in html document. problem is, need search character strings  
. not want code read decoded item, text itself.
admitting being new xml, did make attempts @ meeting requirements. read load of links here on stackoverflow regarding cdata
, attributes
, on, examples here (and elsewhere) seem focus on creating 1 single line in xml file, not multiple.
here 1 of many attempts have made no avail:
<?xml version="1.0" encoding="utf-8" ?> <!doctype item [ <!element item (id, replacewith)> <!element id (#cdata)> <!element replacewith (#cdata)> ]> ]> <item id=" " replacewith=" ">non breaking space</item> <item id="‑" replacewith="-">non breaking hyphen</item>
this document gives me number of errors, including:
- in doctype, errors
<!element id (#cdata)>
. in cdata area, visual studio informs me expecting ',' or '|'. ]>
gives me error ofinvalid token @ root of document
.- and of course, after second
<item
entry, error statingxml document cannot contain multiple root level elements
.
how can write xml file includes multiple items and allows me store , retrieve text within element, rather interpreted characters?
if helps any, using .net, c#, , visual studio.
edit: purpose of xml file provide code list of things search , replace in html file. xml file contains list of what search for
, what replace with
.
here file have in place right now:
<?xml version="1.0" encoding="utf-8" ?> <items> <item id="‑" replacewith="-">non breaking hyphen</item> <item id=" " replacewith=" ">non breaking hyphen</item> </items>
using first example, want read text ‑
instead when read this, -
because code represents.
any or pointers can give helpful.
to elaborate on comment: xml acts html due reserved characters. ampersand prefixes keywords or character codes translate literal string when read in type of parser (browser, xml reader, etc).
the easiest way escape values make sure read in literal want put them in if encoding web. example, create xml document, did this:
xmldocument xmldoc = new xmldocument(); xmlelement xmlitem; xmlattribute xmlattr; xmltext xmltext; // declaration xmldeclaration xmldec = xmldoc.createxmldeclaration("1.0", "utf-8", null); xmlelement xmlroot = xmldoc.documentelement; xmldoc.insertbefore(xmldec, xmlroot); // items xmlelement xmlitems = xmldoc.createelement(string.empty, "items", string.empty); xmldoc.appendchild(xmlitems); // item #1 xmlitem = xmldoc.createelement(string.empty, "item", string.empty); xmlattr = xmldoc.createattribute(string.empty, "id", string.empty); xmlattr.value = "‑"; xmlitem.attributes.append(xmlattr); xmlattr = xmldoc.createattribute(string.empty, "replacewith", string.empty); xmlattr.value = "-"; xmlitem.attributes.append(xmlattr); xmltext = xmldoc.createtextnode("non breaking hyphen"); xmlitem.appendchild(xmltext); xmlitems.appendchild(xmlitem); // item #2 xmlitem = xmldoc.createelement(string.empty, "item", string.empty); xmlattr = xmldoc.createattribute(string.empty, "id", string.empty); xmlattr.value = " "; xmlitem.attributes.append(xmlattr); xmlattr = xmldoc.createattribute(string.empty, "replacewith", string.empty); xmlattr.value = " "; xmlitem.attributes.append(xmlattr); xmltext = xmldoc.createtextnode("non breaking hyphen"); xmlitem.appendchild(xmltext); xmlitems.appendchild(xmlitem); // formatting stringbuilder xmlbuilder = new stringbuilder(); xmlwritersettings xmlsettings = new xmlwritersettings { indent = true, indentchars = " ", newlinechars = "\r\n", newlinehandling = newlinehandling.replace }; using (xmlwriter writer = xmlwriter.create(xmlbuilder, xmlsettings)) { xmldoc.save(writer); } xmloutput.text = xmlbuilder.tostring();
notice put in id
values expecting. now, @ how gets encoded:
<?xml version="1.0" encoding="utf-16"?> <items> <item id="&#8209;" replacewith="-">non breaking hyphen</item> <item id=" " replacewith="&nbsp;">non breaking hyphen</item> </items>
the difference between yours , 1 ampersand encoded &
, rest remained string literal. normal behavior xml. when read in, come literal ‑
,
.
Comments
Post a Comment