c# - What is wrong with this file or code? -
what happening \ difference ? i'm trying return specific node xml file.
xml file:
<?xml version="1.0" encoding="utf-8"?> <jmf senderid="inkzone-controller" version="1.2"> <command id="cmd.00695" type="resource"> <resourcecmdparams resourcename="inkzoneprofile" jobid="k_41"> <inkzoneprofile id="r0013" class="parameter" locked="false" status="available" partidkeys="signaturename sheetname side separation" descriptivename="schieberwerte von di" zonewidth="32"> <inkzoneprofile signaturename="sig1"> <inkzoneprofile locked="false" sheetname="s1"> <inkzoneprofile side="front" /> </inkzoneprofile> </inkzoneprofile> </inkzoneprofile> </resourcecmdparams> </command> <inkzoneprofile separation="cyan" zonesettingsx="0 0,005 " /> </jmf>
code:
xmldocument xmldoc = new xmldocument(); xmldoc.load("c:\\test\\test.xml"); xmlnode root = xmldoc.documentelement; var parent = root.selectsinglenode("/jmf/command/resourcecmdparams/inkzoneprofile/inkzoneprofile/inkzoneprofile/inkzoneprofile"); xmlelement izp = xmldoc.createelement("inkzoneprofile"); izp.setattribute("separation", x.colorname); izp.setattribute("zonesettingsx", x.colorvalues); xmldoc.documentelement.appendchild(izp); xmldoc.save("c:\\test\\test.xml");
the var parent returns me null. i've debugged , , root , xmldoc have on inner text xml content. but, test made here(made user @har07 , on previous question: selectsinglenode returns null namespace managing worked without problems. https://dotnetfiddle.net/vj8h9s
what difference between 2 ? follow same code basically, 1 works , other doesn't.
when debugging i've found root.innerxml has contents loaded on (same xmldoc.innerxml ). innerxml doesn't implement method selectsinglenode. believe if save string i'll lose indentation , etc.
can tell me difference or wrong ? ! xml sample: https://drive.google.com/file/d/0bwu9_grfryrtufhmywk5blhhzwm/view?usp=sharing
setattribute
don't auto escape string you. therefore make xml file invalid.
from msdn xmlelement.setattribute
any markup, such syntax recognized entity reference, treated literal text , needs escaped implementation when written out
find in code line contain setattribute
, use securityelement.escape
escape value.
for example: change these lines:
izp.setattribute("separation", x.colorname); izp.setattribute("zonesettingsx", x.colorvalues);
to:
using system.security; izp.setattribute("separation", securityelement.escape(x.colorname)); izp.setattribute("zonesettingsx", securityelement.escape(x.colorvalues));
if attribute have name contains of <>"'&
have escape value.
note:
you have delete current xmls create used old code, because invalid, when load cause exception.
Comments
Post a Comment