【问题标题】:VB.Net XML error - There are multiple root elementsVB.Net XML 错误 - 有多个根元素
【发布时间】:2016-09-07 07:52:10
【问题描述】:

您好,我正在发送以下 XML,但是我收到错误“第 2 行,位置 2 有多个根元素”我已经检查过了,但找不到任何错误。奇怪的是,这是上周工作的,代码没有改变。

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/" soap:encodingStyle="http://www.w3.org/2003/05/soap-encoding">
  <soap:Body>
    <OTA_VehAvailRateRQ xmlns="http://www.opentravel.org/OTA/2003/05" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opentravel.org/OTA/2003/05 OTA_PingRQ.xsd" Target="Test" Version="1.003">
      <POS>
        <Source ISOCurrency="EUR">
          <RequestorID ID="######"/>
        </Source>
      </POS>
      <VehAvailRQCore Status="Available">
        <VehRentalCore PickUpDateTime="2016-10-10T12:00:00" ReturnDateTime="2016-10-17T12:00:00">
          <PickUpLocation LocationCode="71"/>
          <ReturnLocation LocationCode="71"/>
        </VehRentalCore>
        <DriverType Age="25"/>
      </VehAvailRQCore>
      <VehAvailRQInfo>
        <Customer>
          <Primary>
            <CitizenCountryName Code="IE"/>
          </Primary>
        </Customer>
        <TPA_Extensions>
          <ConsumerIP>###.##.###.###</ConsumerIP>
        </TPA_Extensions>
      </VehAvailRQInfo>
    </OTA_VehAvailRateRQ>
  </soap:Body>
</soap:Envelope>

有没有人知道为什么这不会发送?它甚至没有到达接收服务器。

这是发送 XML 的函数。 XML 被传递一个字符串“request.request”

 Public Function RequestSoapXML(ByVal Request As ALTStructures.ALTXMLStructures.Request) As String
        RequestSoapXML = "Success"
        Try
            Dim webClient As New System.Net.WebClient()
            webClient.Headers.Add("Content-Type", "text/xml;charset=utf-8")
            webClient.Headers.Add("SOAPAction", Request.SOAPAction)
            Dim response = webClient.UploadString(Request.Destination, Request.Request)
            Dim XMLdate As String = System.DateTime.Now.ToString("yyyyMMddHHmmssfff")
            Dim XMLDoc As New XmlDocument
            XMLDoc.LoadXml(response)
            XMLDoc.Save(Request.XMLFile)
        Catch webExcp As System.Net.WebException
            Dim httpResponse As System.Net.HttpWebResponse = CType(webExcp.Response, System.Net.HttpWebResponse)
            Dim StreamObj As Stream = webExcp.Response.GetResponseStream
            Dim SR As New StreamReader(StreamObj, Encoding.UTF8)
            Dim uError As String = SR.ReadToEnd()
            Dim XMLErr As New XmlDocument
            XMLErr.LoadXml(uError)
            XMLErr.Save("###########")
            Dim Xmlreader As XmlTextReader = New XmlTextReader("##########")
            Xmlreader.WhitespaceHandling = WhitespaceHandling.Significant
            Try
                While Not Xmlreader.EOF
                    Select Case Xmlreader.Name
                        Case "faultstring"
                            RequestSoapXML = Xmlreader.ReadInnerXml
                    End Select
                    Xmlreader.Read()
                End While
                Xmlreader.Close()
            Catch ex As Exception
                RequestSoapXML = ex.Message
            End Try
        Catch ex As Exception
            RequestSoapXML = ex.Message
        End Try
    End Function

这个函数处理许多 XML 请求,所以如果错误在这里,我预计会有很多错误。

【问题讨论】:

  • 你能发布给你错误的代码吗? xml 本身对我来说看起来不错,notepad++ 的 xml 语法检查器没有发现任何错误...
  • 嗯,它是有效的 XML,如图所示。我不认为有类似另一部分的内容被错误地添加到末尾,是吗?
  • 这里是发送 XML 的函数。 XML 被传递一个字符串“request.request”

标签: xml vb.net


【解决方案1】:

我找到了问题

这实际上是返回的 XML 的错误,而不是我发送的 XML。

大家干杯。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-15
    • 2017-09-22
    相关资源
    最近更新 更多