BTC
BTC

Reputation: 4062

XmlWriter can't write directly to a URL, so how should I send XML to a URL?

As the question states, I've tried to use XmlWriter with a target defined as an http address which is associated with a shared documents site. How would I send a file there using XmlWriteror otherwise not using XmlWriter?

    Dim resolver As XmlUrlResolver = New XmlUrlResolver()
    resolver.Credentials = CredentialCache.DefaultCredentials

    Dim doc As New XmlDocument
    doc.XmlResolver = resolver

    Dim feedWriter As XmlWriter = XmlWriter.Create("URL")

    Select Case format
        Case FeedFormats.Atom
            Response.ContentType = "application/rss+xml"

            Dim atomFormatter As New Atom10FeedFormatter(feed)
            atomFormatter.WriteTo(feedWriter)
        Case FeedFormats.Rss
            Response.ContentType = "application/atom+xml"

            Dim rssFormatter As New Rss20FeedFormatter(feed)
            rssFormatter.WriteTo(feedWriter)
    End Select

    doc.Save(feedWriter)

    feedWriter.Close()

This is a snippet, I haven't added my declaration of the cases or feed, but that is unnecessary. I suppose this should be done with output streams, but I have no idea how to do this.

Upvotes: 1

Views: 420

Answers (1)

John Saunders
John Saunders

Reputation: 161783

The following is totally untested:

Public Sub WriteXmlToUrl(url As Uri)
    Dim request AS HttpWebRequest = CTYPE(WebRequest.Create(url), HttpWebRequest)
    request.Method = "POST"
    request.ContentType = "application/xml"
    Using stream = request.GetRequestStream
        Using writer = XmlWriter.Create(stream)
            rem Write your Xml
        End Using
    End Using
End Sub

Upvotes: 1

Related Questions