文章出處

 public static void distinct(string filePath)
        {
            //1、創建XML文檔對象
            XmlDocument doc = new XmlDocument();

            //2、加載指定路徑的XML
            doc.Load(filePath);

            //3、獲得根節點
            XmlElement root = doc.DocumentElement;

            //4-1、獲得根節點的所有子節點
            XmlNodeList allNodes = root.ChildNodes;
            for (int i = 0; i < allNodes.Count - 1; i++)
            {
                XmlElement node_Element = (XmlElement)allNodes[i];
                string currentNodeValue = node_Element.GetAttribute("Include");
                for (int j = i + 1; j < allNodes.Count; j++)
                {
                    XmlElement node_Element_temp = (XmlElement)allNodes[j];
                    if (node_Element_temp.GetAttribute("Include") == currentNodeValue)
                    {
                        node_Element_temp.ParentNode.RemoveChild(node_Element_temp);
                        j--;
                        continue;
                    }
                }
            }
            doc.Save(filePath);
        }

調用

 string path = @"..\..\upload\demo.xml";

  distinct(path);

 

<?xml version="1.0" encoding="utf-8"?>
<ItemGroup>
  <Compile Include="1" />
  <Compile Include="1" />
  <Compile Include="1" />
  <Compile Include="2" />
  <Compile Include="3" />
  <Compile Include="1" />
  <Compile Include="1" />
</ItemGroup>

 


文章列表


不含病毒。www.avast.com
全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()