閱讀495 返回首頁    go 阿裏雲 go 技術社區[雲棲]


C# 通過傳入節點name及節點value,來刪除XML相應節點

//通過傳入節點name及節點value,來刪除相應節點
public static string OperateXml(string keyInfo, string valueInfo)
{
    if (File.Exists(filePath))
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(filePath);
        XmlNode xnRoot = xmlDoc.SelectSingleNode("根節點");
        if (xnRoot == null)
        {
            xnRoot = xmlDoc.CreateNode(XmlNodeType.Element, "根節點", "");
            xmlDoc.AppendChild(xnRoot);
        }                       
        XmlNodeList xnl = xmlDoc.SelectSingleNode("根節點").ChildNodes;
        for (int i = 0; i < xnl.Count; i++)
        {
            XmlElement xe = (XmlElement)xnl.Item(i);
            if (xe.Name.Equals(keyInfo))
            {
                xnRoot.RemoveChild(xe);
                if (i < xnl.Count) i = i - 1;
            }
        }
        xmlDoc.Save(filePath);
        return null;
}

最後更新:2017-04-03 12:54:18

  上一篇:go 關於優惠券中經濟學與心理學
  下一篇:go Notepad++ 實用插件安裝