我已经在这墙上打了很长时间了,所以我想我会问一些专家。
我需要从一台计算机向下一台计算机发送一个xml字符串。我想格式化像这样的xml:
<xml>
<author>Joe the Magnificent</author>
<title>Joe Goes Home</title>
</xml>
任何人都可以提供一些帮助吗?
编辑:更多细节
我控制发送和接收,并且已成功地单向传输硬编码字符串。
这是接收方:
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)
大多数情况下,这是一个学习如何为我做XML的练习,所以除了我自己的知识之外,它没有真正的目的。我正在寻找关于做这些事情的最佳方式的一些意见。
答案 0 :(得分:4)
使用XmlDocument.Load方法,您有4个选项:来自Stream,TextReader,URL或XmlReader。
您可以使用NetworkStream课程通过网络。您可以在网站上发布XML并通过URL选项将其删除。您可能希望更具体地了解要进行传输的协议。
例如,要写入流,请使用流的XmlWriter.Create重载。使用XmlWriterSettings对象提供缩进。
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)
构建xml文档[旧方法]非常繁琐,我建议查看VB9 XML文字。但是这里是.NET 2样式XmlDocument操作的一个例子:
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)
答案 1 :(得分:3)
这是我最终做的事情:
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
此函数根据输入值构建xml字符串,然后将xml字符串分解为原始值,我使用了这个:
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)
感谢您的帮助! KJ
答案 2 :(得分:1)
我不知道这是否是您正在寻找的,但如果您使用的是最新版本的VB和.NET,那么您应该能够使用xml文字和LINQ来解析您的xml:喜欢这样 - &gt ;
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
请注意,上面只是空气代码,所以它可能无法运行,而不是在我的电脑上,所以我无法测试它。
答案 3 :(得分:0)
创建一个这样的类:
['a_file.txt', 'another_file.json', 'svg.js', 'a_dir']
然后像这样发送它:
Imports System.Data
Public Class STKReservedStock_insertrow
Inherits Request
Public Sub New(User As String, Company As String)
MyBase.New("stkreservestockall", User, Company)
End Sub
#Region "Properties"
Public Property _pdt As String
Get
Return DirectCast(Field("pdt"), String)
End Get
Set(ByVal value As String)
Field("pdt") = value
End Set
End Property
Public Property _whse As String
Get
Return DirectCast(Field("whse"), String)
End Get
Set(ByVal value As String)
Field("whse") = value
End Set
End Property
Public Property _traceNumber As Integer
Get
Return DirectCast(Field("traceNumber"), Integer)
End Get
Set(ByVal value As Integer)
Field("traceNumber") = value
End Set
End Property
Public Property _bin As String
Get
Return DirectCast(Field("bin"), String)
End Get
Set(ByVal value As String)
Field("bin") = value
End Set
End Property
Public Property _lotref As String
Get
Return DirectCast(Field("lotref"), String)
End Get
Set(ByVal value As String)
Field("lotref") = value
End Set
End Property
Public Property _packUOM As String
Get
Return DirectCast(Field("packUOM"), String)
End Get
Set(ByVal value As String)
Field("packUOM") = value
End Set
End Property
Public Property _grade As String
Get
Return DirectCast(Field("grade"), String)
End Get
Set(ByVal value As String)
Field("grade") = value
End Set
End Property
Public Property _shpLabel As Integer
Get
Return DirectCast(Field("shpLabel"), Integer)
End Get
Set(ByVal value As Integer)
Field("shpLabel") = value
End Set
End Property
Public Property _countLoc As String
Get
Return DirectCast(Field("countLoc"), String)
End Get
Set(ByVal value As String)
Field("countLoc") = value
End Set
End Property
Public Property _palletType As String
Get
Return DirectCast(Field("palletType"), String)
End Get
Set(ByVal value As String)
Field("palletType") = value
End Set
End Property
Public Property _subPdt As String
Get
Return DirectCast(Field("subPdt"), String)
End Get
Set(ByVal value As String)
Field("subPdt") = value
End Set
End Property
Public Property _subTn As Integer
Get
Return DirectCast(Field("subTn"), Integer)
End Get
Set(ByVal value As Integer)
Field("subTn") = value
End Set
End Property
Public Property _origReserved As Decimal
Get
Return DirectCast(Field("origReserved"), Integer)
End Get
Set(ByVal value As Decimal)
Field("origReserved") = value
End Set
End Property
Public Property _reserved As Decimal
Get
Return DirectCast(Field("reserved"), Integer)
End Get
Set(ByVal value As Decimal)
Field("reserved") = value
End Set
End Property
Public Property _dateReserved As Date
Get
Return DirectCast(Field("dateReserved"), Date)
End Get
Set(ByVal value As Date)
Field("dateReserved") = value
End Set
End Property
Public Property _reservedBy As String
Get
Return DirectCast(Field("reservedBy"), String)
End Get
Set(ByVal value As String)
Field("reservedBy") = value
End Set
End Property
Public Property _reason As String
Get
Return DirectCast(Field("reason"), String)
End Get
Set(ByVal value As String)
Field("reason") = value
End Set
End Property
Public Property _party As String
Get
Return DirectCast(Field("party"), String)
End Get
Set(ByVal value As String)
Field("party") = value
End Set
End Property
Public Property _Cancelled As String
Get
Return DirectCast(Field("Cancelled"), String)
End Get
Set(ByVal value As String)
Field("Cancelled") = value
End Set
End Property
Public Property _CancelledByUsr As String
Get
Return DirectCast(Field("CancelledByUsr"), String)
End Get
Set(ByVal value As String)
Field("CancelledByUsr") = value
End Set
End Property
Public Property _RsvQty As Decimal
Get
Return DirectCast(Field("RsvQty"), Integer)
End Get
Set(ByVal value As Decimal)
Field("RsvQty") = value
End Set
End Property
#End Region
End Class
发送显然是您的发送子例程(无论发送它的地方)