我有下面的 XML 文件,我想在第一个节点下添加一个新的子<Profile_Path></Profile_Path>
节点。
原始 XML:
<?xml version="1.0" encoding="utf-8"?>
<Profiles>
<Profile>
<Profile_Name>Profile 1</Profile_Name>
<Profile_Path>E:\Test</Profile_Path>
</Profile>
<Profile>
<Profile_Name>Profile 2</Profile_Name>
<Profile_Path>E:\Test</Profile_Path>
</Profile>
</Profiles>
运行代码后...
Public Sub CreateProjectXml()
ProfileList.Load(xml_path)
Dim profilesNode As XmlNode = ProfileList.SelectSingleNode("Profiles")
Dim profiles As XmlNodeList = profilesNode.SelectNodes("Profile")
Dim profile As XmlNode = profiles(2)
Dim project_info As XmlElement = ProfileList.CreateElement("Project_Name")
project_info.InnerText = "Project 1"
ProfileList.DocumentElement.AppendChild(project_info)
ProfileList.Save(xml_path)
End Sub
我得到以下结果:
<?xml version="1.0" encoding="utf-8"?>
<Profiles>
<Profile>
<Profile_Name>Profile 1</Profile_Name>
<Profile_Path>E:\Test</Profile_Path>
</Profile>
<Profile>
<Profile_Name>Profile 2</Profile_Name>
<Profile_Path>E:\Test</Profile_Path>
</Profile>
<Project_Name>Project 1</Project_Name>
</Profiles>
请帮助我正确的代码!