什么是使用 VB.net 2008 编写 XML 的好例子
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1295603/
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
Whats a Good Example to Write XML using VB.net 2008
提问by Dan Tao
Using this example how would I go about updating an XML file using this example:
使用此示例我将如何使用此示例更新 XML 文件:
<foo>
<n1>
<s1></s1>
<s2></s2>
<s3></s3>
</n1>
<n1>
<s1></s1>
<s2></s2>
<s3></s3>
</n1>
</foo>
I Can read from it all day long but for the life of me I cannot seem to write it back into that format.
我可以整天阅读它,但对于我的生活,我似乎无法将其写回那种格式。
回答by Dan Tao
Straightforward approach:
直截了当的做法:
' to create the XmlDocument... '
Dim xmlDoc As New Xml.XmlDocument
Dim fooElement As Xml.XmlElement = xmlDoc.CreateElement("foo")
xmlDoc.AppendChild(fooElement)
Dim n1Element As Xml.XmlElement = xmlDoc.CreateElement("n1")
For Each n1ChildName As String In New String() {"s1", "s2", "s3"}
Dim childElement As Xml.XmlElement = xmlDoc.CreateElement(n1ChildName)
n1Element.AppendChild(childElement)
Next
fooElement.AppendChild(n1Element)
fooElement.AppendChild(n1Element.CloneNode(deep:=True))
' to update the XmlDocument (simple example)... '
Dim s1Element As Xml.XmlElement = xmlDoc.SelectSingleNode("foo/n1/s1")
If Not s1Element Is Nothing Then s1Element.InnerText = "some value"
回答by Jordan Parmer
Using LINQ-to-XML is a great way to do it in VS2008. Here are some key links:
在 VS2008 中使用 LINQ-to-XML 是一个很好的方法。以下是一些关键链接:
- http://msdn.microsoft.com/en-us/library/bb387098.aspx
- http://msdn.microsoft.com/en-us/library/bb387061.aspx
- http://msdn.microsoft.com/en-us/library/bb387021.aspx
- http://msdn.microsoft.com/en-us/library/bb387098.aspx
- http://msdn.microsoft.com/en-us/library/bb387061.aspx
- http://msdn.microsoft.com/en-us/library/bb387021.aspx
Here is a VB.NET code segment:
这是一个 VB.NET 代码段:
Dim contacts = _
<Contacts>
<Contact>
<Name>Patrick Hines</Name>
<Phone Type="Home">206-555-0144</Phone>
<Phone Type="Work">425-555-0145</Phone>
<Address>
<Street1>123 Main St</Street1>
<City>Mercer Island</City>
<State>WA</State>
<Postal>68042</Postal>
</Address>
</Contact>
</Contacts>
LINQ-to-XML is really simple in VB.NET because it treats it as an XML literal which does the LINQ-to-XML calls behind the scenes. You can directly write the 'contacts' variable above to a file using it's write method.
LINQ-to-XML 在 VB.NET 中非常简单,因为它将其视为在幕后执行 LINQ-to-XML 调用的 XML 文字。您可以使用它的 write 方法将上面的“contacts”变量直接写入文件。
回答by David Salzer
回答by Rowland Shaw
You could also look into XML serialisation, for which you could use something like:
您还可以查看 XML 序列化,为此您可以使用以下内容:
public class foo
{
void bar()
{
System.IO.FileInfo fi = new System.IO.FileInfo("C:\foo.xml")
System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer( typeof( n1 ) );
xs.Serialize(fi.OpenWrite(),new n1());
}
}
public class n1
{
[System.Xml.Serialization.XmlElement()] public string s1 { get; set; }
[System.Xml.Serialization.XmlElement()] public string s2 { get; set; }
[System.Xml.Serialization.XmlElement()] public string s3 { get; set; }
}
回答by Bill
This may give you something to work with...
这可能会给你一些工作......
Sub Main()
Dim oXML As Xml.XmlDocument
Dim oNodes As Xml.XmlNode
Dim oNode As Xml.XmlNode
Dim sFilename As String = "D:\Junk\foo.xml"
oXML = New Xml.XmlDocument
oXML.Load(sFilename)
oNodes = oXML.DocumentElement
oNode = oNodes.ChildNodes(0)
oNode.Item("s1").InnerText = "Pink Floyd"
oNode.Item("s2").InnerText = "Dark Side of the Moon"
oNode.Item("s3").InnerText = "1973"
oNode = oNodes.ChildNodes(1)
oNode.Item("s1").InnerText = "Deep Purple"
oNode.Item("s2").InnerText = "Stormbringer"
oNode.Item("s3").InnerText = "1974"
oXML.Save(sFilename)
End Sub

