日期:2014-05-17  浏览次数:20971 次

C#中查看xml某个节点是否存在..........................新手求解
<?xml version="1.0" encoding="utf-8" ?>
<root>
<left>
<div1></div1>
</left>
<center>

</center>
<right>

</right>
</root>

public static bool isSelectNode(string xmlpath,string node)
{
  ....

  if(存在)
  {
  return true;
  }
  else
  {
  return false;
  }
}

isSelectNode(xxx.xml,"div1");

新手求解,请给出代码。谢谢!

------解决方案--------------------
C# code
    public static bool IsSelectNode(string xmlPath, string node)
    {
        using (XmlReader reader = XmlReader.Create(xmlPath))
        {
            while (reader.Read())
            {
                if (reader.Name == node && reader.NodeType == XmlNodeType.Element)
                    return true;
            }
        }
        return false;
    }