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 5 1305
You'll be delightfully surprised how easy it is. You basically use the
XMLserialization object to create an object or series of nested objects
that can read in or write out XML to a text file or other output
source.
The WROX Visual Basic for Beginners book cover this in detail.
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(Com mandXml))
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
"Scott M. Lyon" <sc******************@rapistan.BLUE.com> wrote in message
news:OI**************@TK2MSFTNGP09.phx.gbl...
| 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
|
|
Hello Scott,
Well, may be I'm simplistic, but if you just want to send a string to a
server, then build the string:
StringBuilder xml=new StringBuilder();
xml.Append("<CommandXML>\n");
xml.Append(" <cmd name=\"1\" action=\"2\">\n");
xml.Append(" <arg name=\"3\" value=\"4\"/>\n");
xml.Append(" <arg name=\"5\" value=\"6\"/>\n");
xml.Append(" </cmd>\n");
xml.Append(" <cmd name=\"7\" action=\"9\">\n");
xml.Append(" <arg name=\"9\" value=\"0\"/>\n");
xml.Append(" </cmd>\n");
xml.Append("</CommandXML>");
Then, when you want to access the string you do a
xml.ToString();
And, if you want to check the Xml you do a:
try {
XmlDocument doc=new XmlDocument();
doc.LoadXml(xml.ToString());
} catch (XmlException e) {
throw new Exception("Error "+e.Message+" in line "+e.LineNumber+"
at\n"+xml.ToString());
}
Hope this helps,
jmgonet
jmgonet wrote: Well, may be I'm simplistic, but if you just want to send a string to a server, then build the string:
Bad idea. Then you must take care of XML syntax issues - well-formdness,
escaping special characters, encoding issues etc etc etc. It's always
much better to let XML API to deal with XML.
StringBuilder xml=new StringBuilder(); xml.Append("<CommandXML>\n");
That's a code from 1998. In 2005 you can have a luxury to use XmlTextWriter.
--
Oleg Tkachenko [XML MVP, MCP] http://blog.tkachenko.com
jmgonet,
As Oleg suggests using a StringBuilder is a "bad" idea. In addition to the
reasons Oleg states. Item #29 "Always Use a Parser" from Elliotte Rusty
Harold's book "Effective XML - 50 Specific Ways to Improve Your XML" from
Addison Wesley lists a number of other reasons to use a parser. Although
Item #29 is largely reading, I find the topic apropos to writing also.
Hope this helps
Jay
"jmgonet" <jm*****@yahoo.com> wrote in message
news:42**********************@news.sunrise.ch...
| Hello Scott,
|
| Well, may be I'm simplistic, but if you just want to send a string to a
| server, then build the string:
|
| StringBuilder xml=new StringBuilder();
| xml.Append("<CommandXML>\n");
| xml.Append(" <cmd name=\"1\" action=\"2\">\n");
| xml.Append(" <arg name=\"3\" value=\"4\"/>\n");
| xml.Append(" <arg name=\"5\" value=\"6\"/>\n");
| xml.Append(" </cmd>\n");
| xml.Append(" <cmd name=\"7\" action=\"9\">\n");
| xml.Append(" <arg name=\"9\" value=\"0\"/>\n");
| xml.Append(" </cmd>\n");
| xml.Append("</CommandXML>");
|
| Then, when you want to access the string you do a
| xml.ToString();
|
| And, if you want to check the Xml you do a:
| try {
| XmlDocument doc=new XmlDocument();
| doc.LoadXml(xml.ToString());
| } catch (XmlException e) {
| throw new Exception("Error "+e.Message+" in line "+e.LineNumber+"
| at\n"+xml.ToString());
| }
|
| Hope this helps,
| jmgonet
|
| This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: J Sharman |
last post by:
What is the easiest way to import an XML Datafeed from a URL into a MYSQL
Database? Possibly using PHP
Regards Joe
PS Please answer to group and joe@joesharman.co.uk
|
by: flamesrock |
last post by:
Lets say I have a list containing 12, 13, 23 or however many entries.
What I want is the greatest number of lists evenly divisible by a
certain number, and for those lists to be assigned to...
|
by: Rolf Hemmerling |
last post by:
Hello !
Beginner's question:
What ist the easiest way to store and save objects in a file generated
by a C++ program, by using the "standard C++ library" and/or "Standard
Template Library (...
|
by: Dan V. |
last post by:
Situation:
I have to connect with my Windows 2000 server using VS.NET 2003 and C# and
connect to a remote Linux server at another company's office and query their
XML file. Their file may be...
|
by: Scott M. Lyon |
last post by:
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...
|
by: DanB |
last post by:
This is probably soooo simple but I can't seem to get it.
I have a text file that I want users to download via a web page.
I want the file to be saved to a default folder (or one that they...
|
by: Craig Buchanan |
last post by:
what is the fastest way to remove a value from a string array? something
like:
dim x as string() = {"A","B","C","D"}
'remove C
x.Clear(x, x.IndexOf(x, "C"), 1)
Questions:
|
by: Petyr David |
last post by:
Just looking for the simplest. right now my perl script returns an
error messge to the user if the date string is invalid. would like to
do this before accessing the server.
TX
|
by: Johnny Jörgensen |
last post by:
Hi
Does anyone know of a component that can facilitate error handling in a
complex application.
What I'm looking for is a component you can simply drop on your application
form (or the like)....
|
by: cbalian |
last post by:
We are currently converting our Microsoft Access reporting to SQL server so we can generate reports out of Crystal Reports instead of Access. Is there a translator or function that you can copy the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |