Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to write and read an xml string

I've been beating my head against this wall for quite some time now, so I thought I'd ask some experts.

I need to send an xml string from one computer to the next. I would like to format the xml something like this:

<xml>
  <author>Joe the Magnificent</author>
  <title>Joe Goes Home</title>
</xml>

Can anyone provide some assistance?

Edit: More detail

I control both the send and receive, and have successfully transfered a hard coded string one-way.

Here is the receive side:

    Dim author As String
    Dim title As String

    Dim xDoc As New XmlDocument
    Dim xAuthor As XmlElement
    Dim xTitle As XmlElement

    xDoc.LoadXml(xml)
    xAuthor = xDoc.FirstChild.Item("author")
    xTitle = xDoc.FirstChild.Item("title")

    author = xAuthor.FirstChild.Value
    title = xTitle.FirstChild.Value

    ShowMessage(author, title)

Mostly this is an exercise in learning how to do XML for me, so there's no real purpose to it other than my own knowledge. I was kind of looking for some opinions on the best way to do such things.

like image 397
Krakerjak Avatar asked Jan 22 '26 21:01

Krakerjak


2 Answers

Using the XmlDocument.Load method you have 4 options: From a Stream, TextReader, URL, or XmlReader.

You could use the NetworkStream class to go over a network. You could post your XML up on a website and suck it down via the URL option. You might want to be more specific about the protocol in which you want the transfer to occur.

For example, to write to a stream use the XmlWriter.Create overload for a stream. Use an XmlWriterSettings object to provide indentation.

   Dim settings As XmlWriterSettings = New XmlWriterSettings()
   settings.Indent = true
   settings.IndentChars = (ControlChars.Tab)
   settings.OmitXmlDeclaration = true

   Dim myNetworkStream As New NetworkStream(mySocket) 'mySocket is a whole other code sample

   ' Create the XmlWriter object and write some content.
   writer = XmlWriter.Create(myNetworkStream, settings)
   XmlDocument.WriteTo(writer)

To construct xml documents [the old way] was quite cumbersome, and I'd suggest looking at VB9 XML literals. However here is an example of .NET 2 style XmlDocument manipulation:

    Dim doc As New XmlDocument()
    Dim root As XmlElement = doc.CreateElement("xml")
    Dim author As XmlElement = doc.CreateElement("author")
    author.Value = "Joe the magnificent"
    Dim title As XmlElement = doc.CreateElement("title")
    title.Value = "Joe goes home"

    root.AppendChild(author)
    root.AppendChild(title)
    doc.AppendChild(root)
like image 129
Jim Burger Avatar answered Jan 24 '26 10:01

Jim Burger


Here's what I ended up doing:

Public Function FormatMessage(ByVal author As String, ByVal title As String, ByVal genre As String) As String
Dim xDoc As New XmlDocument

' Create outer XML
Dim xNode As XmlNode = xDoc.AppendChild(xDoc.CreateElement("xml"))

' Create Author Node
Dim xAuthor As XmlNode = xNode.AppendChild(xDoc.CreateElement("author"))
xAuthor.InnerText = author

' Create Message Node
Dim xTitle As XmlNode = xNode.AppendChild(xDoc.CreateElement("message"))
xtitle.InnerText = title

' Create Genre Node
Dim xGenre As XmlNode = xNode.AppendChild(xDoc.CreateElement("genre"))
xGenre.InnerText = genre

' Create StringWriter to convert XMLDoc to string
Dim xWriter As New IO.StringWriter()
Dim xml_writer As New XmlTextWriter(xWriter)
xDoc.WriteContentTo(xml_writer)
Return xWriter.ToString

End Function

This function builds the xml string based on the input values, then to break the xml string back down into the original values, I used this:

Dim author As String
Dim title As String
Dim genre As String

Dim xDoc As New XmlDocument
Dim xAuthor As XmlElement
Dim xTitle As XmlElement
Dim xGenre as XmlElement

xDoc.LoadXml(xml)
If xDoc.DocumentElement.Name = "xml" Then
    xAuthor = xDoc.FirstChild.Item("author")
    xTitle = xDoc.FirstChild.Item("title")

    author = xAuthor.FirstChild.Value
    title = xTitle.FirstChild.Value
    genre = xGenre.FirstChild.Value
End If

ShowMessage(author, title, genre)

Thanks for the help! KJ

like image 38
Krakerjak Avatar answered Jan 24 '26 11:01

Krakerjak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!