c#下怎樣給xml添加屬性?
c#下怎樣給xml添加屬性?
<?xml version= "1.0 " ?>
<TABLE>
<TR>
<TD> a </TD>
<TD> 68:59 </TD>
<TD> 87 </TD>
</TR>
<TR>
<TD> B </TD>
<TD> 6:19 </TD>
<TD> 80 </TD>
</TR>
</TABLE>
=========================》
<?xml version= "1.0 " ?>
<TABLE>
<TR>
<TD NAME= "TD1 "> a </TD>
<TD NAME= "TD2 "> 68:59 </TD>
<TD NAME= "TD3 "> 87 </TD>
</TR>
<TR>
<TD NAME= "TD1 "> B </TD>
<TD NAME= "TD2 "> 6:19 </TD>
<TD NAME= "TD3 "> 80 </TD>
</TR>
</TABLE>
[解决办法]
参考以下JS代码:
var xmlDoc = new ActiveXObject( "Msxml2.DOMDocument.3.0 ");
var elems;
xmlDoc.async = false;
xmlDoc.resolveExternals = false;
xmlDoc.load( "xxx.xml ");
if (xmlDoc.parseError.errorCode == 0) {
elems= xmlDoc.selectNodes( "//TD ");
for(var i=0;i <elems.length;i++)
elems[i].setAttribute( "NAME ", "TD "+i)
alert(xmlDoc.xml);
}
[解决办法]
nodeElem.SetAttribute( "attr ", "attrValue ");//添加属性节点