c# - How to replace or remove the spl character in xml -
am creating xml file using xmlwriter,i thought works fine when try validate fails bcoz there spl character (bom) appears below,just gone thru forum seen lot of people adive me use "securityelement.escape". not sure how , where.some 1 please guide me remove spl charecter xml file.
<?xml version="1.0" encoding="utf-8"?>
if found code above symbol
it <
my code
xmlwriter xmlwrite; xmlwritersettings settings = new xmlwritersettings(); settings.omitxmldeclaration = true; xmlwrite = xmlwriter.create(@"c:\test.xml",settings); xmlwrite.writestartelement("metadata"); xmlwrite.writeelementstring("story", story); xmlwrite.close();
i can succesfully remove version still spl characters there . stuck now.
any ?
this answer me rid of spl char
xmlwriter xmlwrite; xmlwritersettings settings = new xmlwritersettings(); settings.encoding = new utf8encoding(false);//thanks davee settings.conformancelevel = conformancelevel.fragment; settings.omitxmldeclaration = true; xmlwrite = xmlwriter.create(@"c:\test.xml",settings);
when append used "using (textwriter sw = new streamwriter(@"c:\test.xml")) work out
xmldocument doc = new xmldocument(); doc.load(@"c:\test.xml"); xmlnode newchild = doc.createnode(xmlnodetype.element, "image", null); var attribute = doc.createattribute("name"); attribute.value = nameonly; newchild.attributes.append(attribute); xmlnode xmlelement = doc.createnode(xmlnodetype.element, "width", null); xmlelement.innertext = widthvalue[1].trimstart(); newchild.appendchild(xmlelement); using (textwriter sw = new streamwriter(@"c:\test.xml")) { doc.save(sw); }
when "try validate it", mean? xml validators shouldn't have trouble bom. , escaping bom going produce invalid xml.
if want xmlwritersettings, need create it, set values want, use when create xmlwriter. xmlwriter.settings is read-only.
xmlwritersettings settings = new xmlwritersettings(); settings.conformancelevel = conformancelevel.fragment; settings.omitxmldeclaration = true; writer = xmlwriter.create("c:\test.xml", settings);
that doesn't normal bom there, btw. of bytes correct, there's junk in it. code page set on system?
edit: add encoding xmlwritersettings object:
settings.encoding = new utf8encoding(false);
the 'false' parameter tells encoding not use bom.
Comments
Post a Comment