views:

856

answers:

4

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.

+2  A: 

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)
Jim Burger
A: 

Well I don't know if this is what you are looking for but if you are using the latest version of VB and .NET then you should be able to use xml literals and LINQ to parse your xml: Like so->

Sub Send()
    Dim myxml = <xml>
                   <author>Joe the Magnificent</author>
                   <title>Joe Goes Home</title>
                </xml>
Readxml(myxml)
End Sub

Sub Readxml(myxml as XDocument)
Dim Data = From xml in myxml...<xml> _
           Select New With {.Author = xml.<author>.value, _
                            .title = xml.<title>.value}

For each item in Data
    ShowMessage(item.Author,Item.Title)
Next
End Sub

Note the above is just air code so it may not run, not at my computer so I can't test it.

Nathan W
A: 

I am using TcpClient and TcpListener to pass the xml string over my home network (that works). So my next step was to build the string. It seemed like the logical thing to do was to use some of the built-in xml classes.

After some hacking, I have the read part working (posted above), but the building of the string is throwing me off. I can't even say where to be honest about it, because I've tried multiple combinations and I'm no better off. So, I figured I'd just punt and see what some other people thought were good ways to do it.

Krakerjak
+2  A: 

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

Krakerjak