2

我已经在这堵墙上撞了很长时间了,所以我想我会问一些专家。

我需要将 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 的练习,所以除了我自己的知识之外,它没有真正的目的。我正在寻找一些关于做这些事情的最佳方式的意见。

4

4 回答 4

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)
于 2008-11-05T03:32:52.477 回答
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)

谢谢您的帮助!凯杰

于 2008-11-12T12:37:57.897 回答
1

好吧,我不知道这是否是您要查找的内容,但是如果您使用的是最新版本的 VB 和 .NET,那么您应该能够使用 xml 文字和 LINQ 来解析您的 xml:就像这样->

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

请注意,上面只是空气代码,因此它可能无法运行,而不是在我的计算机上,因此我无法对其进行测试。

于 2008-11-05T03:35:52.563 回答
0

像这样创建一个类:

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

然后像这样发送它:

        Dim XMLRqst As New STKReservedStock_insertrow(User, Company)

    Rqst._pdt = If(IsDBNull(drStock.Item("pdt")), "", drStock.Item("pdt").ToString)
    Rqst._whse = If(IsDBNull(drStock.Item("whse")), "", drStock.Item("whse").ToString)
    Rqst._traceNumber = If(IsDBNull(drStock.Item("traceNumber")), 0, CInt(drStock.Item("traceNumber")))
    Rqst._bin = If(IsDBNull(drStock.Item("bin")), "", drStock.Item("bin").ToString)
    Rqst._lotref = If(IsDBNull(drStock.Item("lotref")), "", drStock.Item("lotref").ToString)
    Rqst._packUOM = If(IsDBNull(drStock.Item("packUOM")), "", drStock.Item("packUOM").ToString)
    Rqst._grade = If(IsDBNull(drStock.Item("grade")), "", drStock.Item("grade").ToString)
    Rqst._shpLabel = If(IsDBNull(drStock.Item("shpLabel")), 0, CInt(drStock.Item("shpLabel")))
    Rqst._countLoc = If(IsDBNull(drStock.Item("countLoc")), "", drStock.Item("countLoc").ToString)
    Rqst._palletType = If(IsDBNull(drStock.Item("palletType")), "", drStock.Item("palletType").ToString)
    Rqst._subPdt = If(IsDBNull(drStock.Item("subPdt")), "", drStock.Item("subPdt").ToString)
    Rqst._subTn = If(IsDBNull(drStock.Item("subTn")), 0, CInt(drStock.Item("subTn")))
    Rqst._origReserved = reservedqty
    Rqst._reserved = reservedqty
    Rqst._dateReserved = Now
    Rqst._reservedBy = User
    Rqst._reason = reason
    Rqst._party = party
    Rqst._Cancelled = "f"
    Rqst._CancelledByUsr = ""
    Rqst._RsvQty = reservedqty

Send(XMLRqst.toxml)

发送显然是您的发送子例程(无论它发送到哪里)

于 2018-12-06T14:30:47.290 回答