vb.net 创建 XML 节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19181023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create XML node
提问by guest
I have the below XML file where I want to add a new child under first <Profile_Path></Profile_Path>node.
我有下面的 XML 文件,我想在第一个<Profile_Path></Profile_Path>节点下添加一个新的子节点。
Original XML:
原始 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>
After running the code...
运行代码后...
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
I get the following result:
我得到以下结果:
<?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>
Help me please with the correct code!
请帮我提供正确的代码!
回答by Steven Doggart
The first problem is that you are appending the child by calling ProfileList.DocumentElement.AppendChild. That method will append the child to the document element, which is the root-level Profileselement. If you want to append the child to the first Profileelement, you need to change it to this:
第一个问题是您通过调用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
Notice in the above example, I changed it to use profiles(0)instead of profiles(2), that way it will use the first one rather than the third one.
请注意,在上面的示例中,我将其更改为使用profiles(0)而不是profiles(2),这样它将使用第一个而不是第三个。
However, it is worth mentioning that SelectNodesand SelectSingleNodeuse XPath. That means you can simplify your logic considerably by only selecting the one element you really want, for instance, if all you want is the very first Profileelement, you could just do this:
不过,值得一提的是,SelectNodes并SelectSingleNode使用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
The SelectSingleNodemethod will only return the first matching element anyway, so you don't need to specify the index in the XPath, but if you want to be more explicit, you could specify the index to only get the first one, like this:
SelectSingleNode无论如何,该方法只会返回第一个匹配的元素,因此您不需要在 XPath 中指定索引,但如果您想更明确,您可以指定索引以仅获取第一个,如下所示:
Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile[1]")
Or, if you wanted to get the third Profileelement, you could use this XPath instead:
或者,如果您想获得第三个Profile元素,您可以改用此 XPath:
Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile[3]")
Or, if you wanted to get the Profileelement which had a Profile_Nameequal to "Profile 2", you could just do this:
或者,如果您想获得等于“Profile 2”的Profile元素Profile_Name,您可以这样做:
Dim profile As XmlNode = ProfileList.SelectSingleNode("Profiles/Profile[Profile_Name='Profile 2']")
Etc. If you are going to be working with XML much, it would be well worth your effort to spend some time learning the basics of XPath. XPath is a standard XML query language which is used in many XML tools and programming languages. For instance, if you are going to ever use XSLT, you'll need to understand XPath. XPath can be used with the XmlDocumentclass, as I mentioned above, and also with the newer XDocumentclass.
等等。如果您打算大量使用 XML,那么花一些时间学习 XPath 的基础知识是非常值得的。XPath 是一种标准的 XML 查询语言,用于许多 XML 工具和编程语言。例如,如果您打算使用 XSLT,则需要了解 XPath。XmlDocument正如我上面提到的,XPath 可以与类一起使用,也可以与较新的XDocument类一起使用。
The alternative to XPath is to use LINQ. LINQ is a proprietary Microsoft technology, so you won't find any support for it in other tools and languages outside of .NET, but many people do prefer it. The new XDocumentclass is designed to make XML easy to work with via LINQ. Combined with VB.NET's support for inline XML literals, this task is actually quite easy with XDocument:
XPath 的替代方法是使用 LINQ。LINQ 是一项专有的 Microsoft 技术,因此您不会在 .NET 之外的其他工具和语言中找到对它的任何支持,但许多人确实更喜欢它。新XDocument类旨在使 XML 易于通过 LINQ 使用。结合 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)
回答by Christos
Alex I think you shoud use the following code
亚历克斯我认为你应该使用以下代码
ProfileList.DocumentElement.InsertAfter(project_info, profiles.FirstChild)
Insted of
插入的
ProfileList.DocumentElement.AppendChild(project_info)
回答by sloth
Since you're using VB.Net, you can work with XML the easy way.
由于您使用的是 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>)
Now xmlis
现在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>
so you basically just need this:
所以你基本上只需要这个:
Public Sub CreateProjectXml()
ProfileList.Load(xml_path)
ProfileList...<Profile>.First().Add(<Project_Name>Project 1</Project_Name>)
ProfileList.Save(xml_path)
End Sub

