locked
Read XML Element Content with Line Break RRS feed

  • Question

  • User-1495932509 posted

    Hello, Hope all is well with you all, I am trying to read an xml element content sent by a client but unfortunately it does not read the xml request with line break. Please how can I read the xml request with or without XML request. Please find my code bellow.

    Protected CustomerID, CompanyName, ContactName, ContactTitle, Address, City, PostalCode, Country, Phone, Fax As String
        Protected xmlHeader As String = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
        Dim xmlDoc As New XmlDocument
        Dim XMLreader As XmlTextReader
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            
            'Read the input stream to get incoming SOAP message
            Dim sr As New StreamReader(System.Web.HttpContext.Current.Request.InputStream)
            Dim Results As String = System.Web.HttpContext.Current.Server.UrlDecode(sr.ReadToEnd().Trim)
            ''''TODO: Check if There is Request Presence
            If Results <> "" Then
                                        
                XMLreader = New XmlTextReader(New System.IO.StringReader(Results))
                 
                Using XMLreader
                    While XMLreader.Read()
                        If XMLreader.IsStartElement() Then
                            'return only when you have START tag
                            Select Case XMLreader.Name.ToString
     
                                Case "CustomerID"
                                    ' Detect this element.
                                    CustomerID = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "CompanyName"
                                    ' Detect this element.
                                    CompanyName = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "ContactName"
                                    ' Detect this element.
                                    ContactName = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "ContactTitle"
                                    ' Detect this element.
                                    ContactTitle = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "Address"
                                    ' Detect this element.
                                    Address = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "City"
                                    ' Detect this element.
                                    City = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "PostalCode"
                                    ' Detect this element.
                                    PostalCode = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "Country"
                                    ' Detect this element.
                                    Country = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "Phone"
                                    ' Detect this element.
                                    Phone = XMLreader.ReadInnerXml()
                                    Exit Select
                                Case "Fax"
                                    ' Detect this element.
                                    Fax = XMLreader.ReadInnerXml()
                                    Exit Select
                                                                                                
                            End Select
                        End If
                    End While
                End Using
                 
            Else
                'TODO: SEND EMPTY REQUEST
                SendEmpTy_Request()
                 
            End If
             
            'TODO: CALL RESPONSE
            ResponceMessage()
             
             
             
        End Sub
         
         
        Protected Sub ResponceMessage()
            System.Web.HttpContext.Current.Response.ContentType = "text/xml"
            Dim sb As New StringBuilder()
            sb.Append(xmlHeader)
            
            sb.Append("<CustomerID>")
            sb.Append(CustomerID)
            sb.Append("</CustomerID>")
             
            sb.Append("<CompanyName>")
            sb.Append(CompanyName)
            sb.Append("</CompanyName>")
             
            sb.Append("<ContactName>")
            sb.Append(ContactName)
            sb.Append("</ContactName>")
             
            sb.Append("<ContactTitle>")
            sb.Append(ContactTitle)
            sb.Append("</ContactTitle>")
             
             
            sb.Append("<Address>")
            sb.Append(Address)
            sb.Append("</Address>")
             
            sb.Append("<City>")
            sb.Append(City)
            sb.Append("</City>")
             
            sb.Append("<PostalCode>")
            sb.Append(PostalCode)
            sb.Append("</PostalCode>")
             
            sb.Append("<Country>")
            sb.Append(Country)
            sb.Append("</Country>")
             
            sb.Append("<Phone>")
            sb.Append(Phone)
            sb.Append("</Phone>")
             
            sb.Append("<Fax>")
            sb.Append(Fax)
            sb.Append("</Fax>")
             
            System.Web.HttpContext.Current.Response.Write(sb.ToString())
        End Sub
         
        Protected Sub SendEmpTy_Request()
            System.Web.HttpContext.Current.Response.ContentType = "text/xml"
            Dim sb As New StringBuilder()
            sb.Append(xmlHeader)
            sb.Append("<SystemResponse>")
            sb.Append("Empty SOAP Request")
            sb.Append("</SystemResponse>")
            System.Web.HttpContext.Current.Response.Write(sb.ToString())
        End Sub

    XML request With Line Break

    <?xml version="1.0" standalone="yes"?><Table><CustomerID>ALFKI
    </CustomerID><CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria </ContactName><ContactTitle>Sales Representa
    tive</ContactTitle><Address>Obere Str. 57</Address><City>Boise</City><PostalCode>12209
    </PostalCode><Country>Germany</Country><Phone>030-00743
    21</Phone><Fax>030-0076545</Fax></Table>

    XML Request With no Line Break

    <?xml version="1.0" standalone="yes"?>
     <Table>
        <CustomerID>ALFKI</CustomerID>
        <CompanyName>Alfreds Futterkiste</CompanyName>
        <ContactName>Maria </ContactName>
        <ContactTitle>Sales Representative</ContactTitle>
        <Address>Obere Str. 57</Address>
        <City>Boise</City>
        <PostalCode>12209</PostalCode>
        <Country>Germany</Country>
        <Phone>030-0074321</Phone>
        <Fax>030-0076545</Fax>
     </Table>

    Response With Line Break

    <?xml version="1.0" encoding="utf-8" ?>
    <CustomerID>ALFKI
    </CustomerID>
    <CompanyName></CompanyName>
    <ContactName>Maria </ContactName>
    <ContactTitle></ContactTitle>
    <Address>Obere Str. 57</Address>
    <City></City>
    <PostalCode>12209
    </PostalCode>
    <Country></Country>
    <Phone>030-00743
    21</Phone>
    <Fax></Fax>

    Response Without Line Break

    <?xml version="1.0" encoding="utf-8" ?>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <ContactName>Maria </ContactName>
    <ContactTitle>Sales Representative</ContactTitle>
    <Address>Obere Str. 57</Address>
    <City>Boise</City>
    <PostalCode>12209</PostalCode>
    <Country>Germany</Country>
    <Phone>030-0074321</Phone>
    <Fax>030-0076545</Fax>

    IT seems the Response With Line Break has missing Information in some element. please how can i format the request before reading the content , may be information in the element may be conplete. Thank you very much

    Wednesday, February 7, 2018 6:18 AM

All replies

  • User-943250815 posted

    Just a clue. What happens if load all in a XMLDocument then use XPath to get values you need.
    You can try XMLDocument.Load(your XMLReader) or XMLDocument.LoadXML(your XML string)

    Wednesday, February 7, 2018 10:35 PM