Scott,
As Bingomanatee suggests XML Serialization is one of the easier ways,
especially if you can represent your data as Objects. The trick is getting
the "arrays" correct (the list of commands & the list of arguments).
Something like (note the sample only supports a single cmd & a single arg):
Imports System.Xml.Serialization
Dim writer As New System.Xml.XmlTextWriter("CommandXml.xml",
System.Text.Encoding.UTF8)
writer.Formatting = Xml.Formatting.Indented
writer.Indentation = 1
writer.IndentChar = ControlChars.Tab
Dim serializer As New
System.Xml.Serialization.XmlSerializer(GetType(CommandXml))
Dim command As New CommandXml
command.Command = New command
command.Command.Name = "1"
command.Command.Action = "2"
command.Command.Argument = New Argument
command.Command.Argument.Name = "3"
command.Command.Argument.Value = "4"
serializer.Serialize(writer, command)
writer.Close()
Public Class CommandXml
Private m_command As Command
<XmlElement("cmd")> _
Public Property Command() As Command
Get
Return m_command
End Get
Set(ByVal value As Command)
m_command = value
End Set
End Property
End Class
Public Class Command
Private m_name As String
Private m_action As String
Private m_argument As Argument
<XmlAttributeAttribute("name")> _
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
<XmlAttributeAttribute("action")> _
Public Property Action() As String
Get
Return m_action
End Get
Set(ByVal value As String)
m_action = value
End Set
End Property
<XmlElement("arg")> _
Public Property Argument() As Argument
Get
Return m_argument
End Get
Set(ByVal value As Argument)
m_argument = value
End Set
End Property
End Class
Public Class Argument
Private m_name As String
Private m_value As String
<XmlAttributeAttribute("name")> _
Public Property Name() As String
Get
Return m_name
End Get
Set(ByVal value As String)
m_name = value
End Set
End Property
<XmlAttributeAttribute("value")> _
Public Property Value() As String
Get
Return m_value
End Get
Set(ByVal value As String)
m_value = value
End Set
End Property
End Class
I find using System.Text.XmlTextWriter to be equally easy. Something like:
Dim writer As New System.Xml.XmlTextWriter("CommandXml.xml",
System.Text.Encoding.UTF8)
writer.Formatting = Xml.Formatting.Indented
writer.Indentation = 1
writer.IndentChar = ControlChars.Tab
writer.WriteStartDocument()
' <CommandXML>
writer.WriteStartElement("CommandXML")
' <cmd name="1" action="2">
writer.WriteStartElement("cmd")
writer.WriteAttributeString("name", "1")
writer.WriteAttributeString("action", "2")
' <arg name="3" value = "4"/>
writer.WriteStartElement("arg")
writer.WriteAttributeString("name", "3")
writer.WriteAttributeString("value", "4")
writer.WriteEndElement() ' arg
' <arg name="5" value = "6"/>
writer.WriteStartElement("arg")
writer.WriteAttributeString("name", "5")
writer.WriteAttributeString("value", "6")
writer.WriteEndElement() ' arg
' </cmd>
writer.WriteEndElement() ' cmd
' <cmd name="7" action="9">
writer.WriteStartElement("cmd")
writer.WriteAttributeString("name", "7")
writer.WriteAttributeString("action", "9")
' <arg name="9" value = "0"/>
writer.WriteStartElement("arg")
writer.WriteAttributeString("name", "9")
writer.WriteAttributeString("value", "0")
writer.WriteEndElement() ' arg
' </cmd>
writer.WriteEndElement() ' cmd
' </CommandXML>
writer.WriteEndElement() ' CommandXML
writer.WriteEndDocument()
writer.Close()
Hope this helps
Jay
| Quick (hopefully easy) question for you guys.
|
|
| What is going to be the quickest/easiest way to generate XML from VB.NET?
|
|
| Note: I don't mean an XML file, but XML in memory somehow (an XML-related
| object, but one that would have a method for getting the fully-formed XML
| back out again).
|
|
| For example, what will be my easiest way to build the following in memory:
|
| <CommandXML>
| <cmd name="1" action="2">
| <arg name="3" value = "4"/>
| <arg name="5" value = "6"/>
| </cmd>
| <cmd name="7" action="9">
| <arg name="9" value = "0"/>
| </cmd>
| </CommandXML>
|
|
|
| Basically, the reason I need this in memory, is I need to build this XML,
| then send it (as a string) to a server component.
|
|
| Thanks!
| -Scott
|
|