xml - 什么是使用 VB.net 2008 编写 XML 的好例子

标签 xml vb.net visual-studio-2008 linq-to-xml

使用此示例,我将如何使用此示例更新 XML 文件:

<foo>
   <n1>
       <s1></s1>
       <s2></s2>
       <s3></s3>
   </n1>
   <n1>
       <s1></s1>
       <s2></s2>
       <s3></s3>
   </n1>
</foo>

我可以整天阅读它,但在我的一生中,我似乎无法将它写回那种格式。

最佳答案

直接的方法:

' 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"

关于xml - 什么是使用 VB.net 2008 编写 XML 的好例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1295603/

相关文章:

c# - 从字符串中删除所有特殊字符

vb.net - 将 DayOfWeek 枚举转换为表示日期的字符串

visual-studio-2008 - Visual Studio 单元测试中的 HttpRuntime

.net - 在 Linq To SQL 中更新的最佳方式

xml - 在 XML 架构中使用实体 'constants'

java - android xml错误: 'class' or 'interface' required

C# XML反序列化错误(2,2)

vb.net - DataGridView 中整数的输入验证

c++ - Visual Studio 2010 & 2008 无法处理不同文件夹中同名的源文件?

使用分隔符时的 XML 格式编号问题