2

我有下面的 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>

请帮助我正确的代码!

4

3 回答 3

3

第一个问题是您通过调用ProfileList.DocumentElement.AppendChild. 该方法会将子元素附加到文档元素,即根级Profiles元素。如果要将子元素附加到第一个Profile元素,则需要将其更改为:

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(0)
    Dim project_info As XmlElement = ProfileList.CreateElement("Project_Name")
    project_info.InnerText = "Project 1"
    profile.AppendChild(project_info)
    ProfileList.Save(xml_path)
End Sub

请注意,在上面的示例中,我将其更改为 useprofiles(0)而不是profiles(2),这样它将使用第一个而不是第三个。

但是,值得一提的是SelectNodesSelectSingleNode使用了 XPath。这意味着您可以通过仅选择您真正想要的一个元素来大大简化您的逻辑,例如,如果您想要的只是第一个Profile元素,您可以这样做:

Public Sub CreateProjectXml()
    ProfileList.Load(xml_path)
    Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile")
    Dim project_info As XmlElement = ProfileList.CreateElement("Project_Name")
    project_info.InnerText = "Project 1"
    profile.AppendChild(project_info)
    ProfileList.Save(xml_path)
End Sub

无论如何,该SelectSingleNode方法只会返回第一个匹配元素,因此您无需在 XPath 中指定索引,但如果您想更明确,您可以指定索引以仅获取第一个,如下所示:

Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile[1]")

或者,如果你想获得第三个Profile元素,你可以使用这个 XPath:

Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile[3]")

或者,如果您想获得等于“配置文件 2”的Profile元素,您可以这样做:Profile_Name

Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile[Profile_Name='Profile 2']")

等等。如果您打算大量使用 XML,那么花一些时间学习 XPath 的基础知识是非常值得的。XPath 是一种标准的 XML 查询语言,用于许多 XML 工具和编程语言。例如,如果您打算使用 XSLT,则需要了解 XPath。XmlDocument如上所述,XPath 可以与类一起使用,也可以与较新的XDocument类一起使用。

XPath 的替代方法是使用 LINQ。LINQ 是一项专有的 Microsoft 技术,因此您不会在 .NET 之外的其他工具和语言中找到对它的任何支持,但许多人确实更喜欢它。新XDocument类旨在通过 LINQ 使 XML 易于使用。结合 VB.NET 对内联 XML 文字的支持,这项任务实际上非常简单XDocument

Dim doc As XDocument = XDocument.Load(xml_path)
doc.<Profiles>.<Profile>(0).Add(<Project_Name>Project 1</Project_Name>)
doc.Save(xml_path)
于 2013-10-04T12:46:36.590 回答
0

亚历克斯我认为你应该使用以下代码

ProfileList.DocumentElement.InsertAfter(project_info, profiles.FirstChild)

安装在

ProfileList.DocumentElement.AppendChild(project_info)
于 2013-10-04T12:27:19.243 回答
0

由于您使用的是 VB.Net,因此您可以轻松地使用 XML。

Dim 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>

xml...<Profile>.First().Add(<Project_Name>Project 1</Project_Name>)

现在xml

<Profiles>
  <Profile>
    <Profile_Name>Profile 1</Profile_Name>
    <Profile_Path>E:\Test</Profile_Path>
    <Project_Name>Project 1</Project_Name>
  </Profile>
  <Profile>
    <Profile_Name>Profile 2</Profile_Name>
    <Profile_Path>E:\Test</Profile_Path>
  </Profile>
</Profiles>

所以你基本上只需要这个:

Public Sub CreateProjectXml()
    ProfileList.Load(xml_path)
    ProfileList...<Profile>.First().Add(<Project_Name>Project 1</Project_Name>)
    ProfileList.Save(xml_path)
End Sub
于 2013-10-04T12:32:37.390 回答