日期:2014-05-20  浏览次数:20431 次

送分!如何判断XML的节点没有某个属性?在线等待。。。。
XML文件
<?xml   version= "1.0 "   standalone= "yes "?>
<Structrue>
    <Departments>
        <Department   DWBM= "115600200101 "   DWMC= "a "   DWYWSX= "SIPC "   />
        <Department   DWBM= "100300200601 "   DWMC= "b "   SJDWBM= "1150 "   />
        <Department   DWBM= "200000200601 "   DWMC= "c "   SJDWBM= "1156 "   />
        <Department   DWBM= "168200200201 "   DWMC= "d "   DWYWSX= "SSG "   SJDWBM= "1003 "   />

    </Departments>
</Structrue>

问题:
怎么样找到没有属性“SJDWBM”的节点?并且在这个节点添加“SJDWBM”属性??

------解决方案--------------------
XmlNode s = doc.SelectSingleNode( "//Department/@SJDWBM ");
if(s == null)
{
//没有
}
------解决方案--------------------
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(Server.MapPath( "a.xml "));
System.Xml.XmlNodeList nodes = doc.SelectNodes( "/Structrue/Departments/Department ");
foreach (System.Xml.XmlNode n in nodes)
{
if (n.Attributes[ "SJDWBM "] == null)
{
System.Xml.XmlAttribute t = doc.CreateAttribute( "SJDWBM ");
t.Value = "xxxxx ";
n.Attributes.Append(t);
}
}

Response.Write(Server.HtmlEncode(doc.OuterXml));
------解决方案--------------------
string strXML = @ " <?xml version= " "1.0 " " standalone= " "yes " "?> <Structrue> <Departments> <Department DWBM= " "115600200101 " " DWMC= " "a " " DWYWSX= " "SIPC " " /> <Department DWBM= " "100300200601 " " DWMC= " "b " " SJDWBM= " "1150 " " /> <Department DWBM= " "200000200601 " " DWMC= " "c " " SJDWBM= " "1156 " " /> <Department DWBM= " "168200200201 " " DWMC= " "d " " DWYWSX= " "SSG " " SJDWBM= " "1003 " " /> </Departments> </Structrue> "; XmlDocument dom = new XmlDocument(); dom.LoadXml(strXML); XmlNodeList nl = dom.SelectNodes( "//*[string-length(@SJDWBM)=0] "); for(int i=0;i <nl.Count;i++) { //Response.Write(nl[i].Name.ToString()+ " <BR> "); ((XmlElement) nl[i]).SetAttribute( "SJDWBM ",i.ToString()); } dom.Save(Response.OutputStream); Response.End();