删除XML子节点时为什么会剩下一空节点?XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("c:\aa.xml"); XmlNodeList xnl=xmlDoc.SelectSingleNode("//DATAPACKET/ROWDATA").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("gdsid")=="???")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("gdsid")=="6")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("c:\aa.xml");
textBox1.Text = xmlDoc.OuterXml;
}删除后文件如下:<?xml version="1.0" encoding="gb2312"?>
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD FieldName="gdsid" DisplayLabel="gdsid" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="gdsname" DisplayLabel="gdsname" FieldType="String" FieldClass="TField" />
<FIELD FieldName="gdstype" DisplayLabel="gdstype" FieldType="String" FieldClass="TField" />
<FIELD FieldName="Unit" DisplayLabel="Unit" FieldType="String" FieldClass="TField" />
<FIELD FieldName="PackType" DisplayLabel="PackType" FieldType="String" FieldClass="TField" />
<FIELD FieldName="classcode" DisplayLabel="classcode" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="brandcode" DisplayLabel="brandcode" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="cyc" DisplayLabel="cyc" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="ptaxrate" DisplayLabel="ptaxrate" FieldType="Float" FieldClass="TField" />
<FIELD FieldName="staxrate" DisplayLabel="staxrate" FieldType="Float" FieldClass="TField" />
<FIELD FieldName="obarcode" DisplayLabel="obarcode" FieldType="String" FieldClass="TField" />
<FIELD FieldName="ogdscode" DisplayLabel="ogdscode" FieldType="Integer" FieldClass="TField" />
</FIELDS>
</METADATA>
<ROWDATA>
<ROW gdsid="1" gdsname="雷登烟嘴" gdstype="空" Unit="盒" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000070588" ogdscode="91,246" />
<ROW gdsid="2" gdsname="雷登烟焦油滤皿" gdstype="空" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000070502" ogdscode="94,240" />
<ROW gdsid="3" gdsname="320#精烟咀" gdstype="1*12" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000071035" ogdscode="94,243" />
<ROW gdsid="4" gdsname="100#滤芯" gdstype="2*24" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000071080" ogdscode="94,245" />
<ROW gdsid="5" gdsname="140#喷咀" gdstype="1*12" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000071134" ogdscode="94,247" />
<ROW />
</ROWDATA>
</DATAPACKET>
xmlDoc.Load("c:\aa.xml"); XmlNodeList xnl=xmlDoc.SelectSingleNode("//DATAPACKET/ROWDATA").ChildNodes;
foreach(XmlNode xn in xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("gdsid")=="???")
{
xe.RemoveAttribute("genre");//删除genre属性
}
else if(xe.GetAttribute("gdsid")=="6")
{
xe.RemoveAll();//删除该节点的全部内容
}
}
xmlDoc.Save("c:\aa.xml");
textBox1.Text = xmlDoc.OuterXml;
}删除后文件如下:<?xml version="1.0" encoding="gb2312"?>
<DATAPACKET Version="2.0">
<METADATA>
<FIELDS>
<FIELD FieldName="gdsid" DisplayLabel="gdsid" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="gdsname" DisplayLabel="gdsname" FieldType="String" FieldClass="TField" />
<FIELD FieldName="gdstype" DisplayLabel="gdstype" FieldType="String" FieldClass="TField" />
<FIELD FieldName="Unit" DisplayLabel="Unit" FieldType="String" FieldClass="TField" />
<FIELD FieldName="PackType" DisplayLabel="PackType" FieldType="String" FieldClass="TField" />
<FIELD FieldName="classcode" DisplayLabel="classcode" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="brandcode" DisplayLabel="brandcode" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="cyc" DisplayLabel="cyc" FieldType="Integer" FieldClass="TField" />
<FIELD FieldName="ptaxrate" DisplayLabel="ptaxrate" FieldType="Float" FieldClass="TField" />
<FIELD FieldName="staxrate" DisplayLabel="staxrate" FieldType="Float" FieldClass="TField" />
<FIELD FieldName="obarcode" DisplayLabel="obarcode" FieldType="String" FieldClass="TField" />
<FIELD FieldName="ogdscode" DisplayLabel="ogdscode" FieldType="Integer" FieldClass="TField" />
</FIELDS>
</METADATA>
<ROWDATA>
<ROW gdsid="1" gdsname="雷登烟嘴" gdstype="空" Unit="盒" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000070588" ogdscode="91,246" />
<ROW gdsid="2" gdsname="雷登烟焦油滤皿" gdstype="空" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000070502" ogdscode="94,240" />
<ROW gdsid="3" gdsname="320#精烟咀" gdstype="1*12" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000071035" ogdscode="94,243" />
<ROW gdsid="4" gdsname="100#滤芯" gdstype="2*24" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000071080" ogdscode="94,245" />
<ROW gdsid="5" gdsname="140#喷咀" gdstype="1*12" Unit="合" PackType="" classcode="692" brandcode="0" cyc="15" ptaxrate="0.17" staxrate="0.17" obarcode="2008000071134" ogdscode="94,247" />
<ROW />
</ROWDATA>
</DATAPACKET>
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货