Updating an XML file from a URL

Discussion in 'ASP .Net' started by =?Utf-8?B?Sm9lbA==?=, Oct 21, 2005.

  1. Hi,

    I have an XML file at http://localhost/test.xml that I want to read and then
    update.
    I can get and read the file no problem with GetResponseStream etc...

    The code (see 2 examples below) I am using to write the new xml to that same
    location does not give me any error but the file never updates. When I open
    the file from XMLSPY with Open URL ... I can read and update it.

    Thanks for your help.

    First example

    Dim myRequest As HttpWebRequest =
    CType(WebRequest.Create("http://localhost/test.xml"), HttpWebRequest)

    myRequest.Method = "POST"
    myRequest.PreAuthenticate = True
    Dim networkCredential As New NetworkCredential("myName", "MyPassword",
    "MyDomain")

    myRequest.Credentials = networkCredential

    ' this one doesn't work either myRequest.ContentType =
    "application/x-www-form-urlencoded"
    myRequest.ContentType = "text/xml"

    Dim newStream As Stream = myRequest.GetRequestStream()
    Dim sw As New StreamWriter(newStream)
    sw.Write("<test></test>")
    sw.Flush()
    sw.Close()

    Second example

    Dim myRequest As WebRequest =
    HttpWebRequest.Create("http://localhost/test.xml")
    myRequest.Method = "POST"
    myRequest.ContentType = "text/xml"
    myRequest.PreAuthenticate = True
    Dim networkCredential As New NetworkCredential("myName", "myPassword",
    "MyDomain")

    myRequest.Credentials = networkCredential

    Dim postData As String = "<test></test>"
    Dim encoder As New ASCIIEncoding
    Dim byteArray As Byte() = encoder.GetBytes(postData)
    myRequest.ContentLength = byteArray.Length

    Dim readStream As Stream = myRequest.GetRequestStream()

    readStream.Write(byteArray, 0, postData.Length)
    readStream.Close()
     
    =?Utf-8?B?Sm9lbA==?=, Oct 21, 2005
    #1
    1. Advertising

  2. Try writing to a file location on the server instead of the RequestStream
    coming from the browser.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.

    "Joel" <> wrote in message
    news:...
    > Hi,
    >
    > I have an XML file at http://localhost/test.xml that I want to read and
    > then
    > update.
    > I can get and read the file no problem with GetResponseStream etc...
    >
    > The code (see 2 examples below) I am using to write the new xml to that
    > same
    > location does not give me any error but the file never updates. When I
    > open
    > the file from XMLSPY with Open URL ... I can read and update it.
    >
    > Thanks for your help.
    >
    > First example
    >
    > Dim myRequest As HttpWebRequest =
    > CType(WebRequest.Create("http://localhost/test.xml"), HttpWebRequest)
    >
    > myRequest.Method = "POST"
    > myRequest.PreAuthenticate = True
    > Dim networkCredential As New NetworkCredential("myName", "MyPassword",
    > "MyDomain")
    >
    > myRequest.Credentials = networkCredential
    >
    > ' this one doesn't work either myRequest.ContentType =
    > "application/x-www-form-urlencoded"
    > myRequest.ContentType = "text/xml"
    >
    > Dim newStream As Stream = myRequest.GetRequestStream()
    > Dim sw As New StreamWriter(newStream)
    > sw.Write("<test></test>")
    > sw.Flush()
    > sw.Close()
    >
    > Second example
    >
    > Dim myRequest As WebRequest =
    > HttpWebRequest.Create("http://localhost/test.xml")
    > myRequest.Method = "POST"
    > myRequest.ContentType = "text/xml"
    > myRequest.PreAuthenticate = True
    > Dim networkCredential As New NetworkCredential("myName", "myPassword",
    > "MyDomain")
    >
    > myRequest.Credentials = networkCredential
    >
    > Dim postData As String = "<test></test>"
    > Dim encoder As New ASCIIEncoding
    > Dim byteArray As Byte() = encoder.GetBytes(postData)
    > myRequest.ContentLength = byteArray.Length
    >
    > Dim readStream As Stream = myRequest.GetRequestStream()
    >
    > readStream.Write(byteArray, 0, postData.Length)
    > readStream.Close()
    >
     
    Kevin Spencer, Oct 21, 2005
    #2
    1. Advertising

  3. I can try but I was testing this because I'm planning to access xml files
    stored in a sharepoint document library and the link to those isn't a file
    location.

    "Kevin Spencer" wrote:

    > Try writing to a file location on the server instead of the RequestStream
    > coming from the browser.
    >
    > --
    > HTH,
    >
    > Kevin Spencer
    > Microsoft MVP
    > ..Net Developer
    > Ambiguity has a certain quality to it.
    >
    > "Joel" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I have an XML file at http://localhost/test.xml that I want to read and
    > > then
    > > update.
    > > I can get and read the file no problem with GetResponseStream etc...
    > >
    > > The code (see 2 examples below) I am using to write the new xml to that
    > > same
    > > location does not give me any error but the file never updates. When I
    > > open
    > > the file from XMLSPY with Open URL ... I can read and update it.
    > >
    > > Thanks for your help.
    > >
    > > First example
    > >
    > > Dim myRequest As HttpWebRequest =
    > > CType(WebRequest.Create("http://localhost/test.xml"), HttpWebRequest)
    > >
    > > myRequest.Method = "POST"
    > > myRequest.PreAuthenticate = True
    > > Dim networkCredential As New NetworkCredential("myName", "MyPassword",
    > > "MyDomain")
    > >
    > > myRequest.Credentials = networkCredential
    > >
    > > ' this one doesn't work either myRequest.ContentType =
    > > "application/x-www-form-urlencoded"
    > > myRequest.ContentType = "text/xml"
    > >
    > > Dim newStream As Stream = myRequest.GetRequestStream()
    > > Dim sw As New StreamWriter(newStream)
    > > sw.Write("<test></test>")
    > > sw.Flush()
    > > sw.Close()
    > >
    > > Second example
    > >
    > > Dim myRequest As WebRequest =
    > > HttpWebRequest.Create("http://localhost/test.xml")
    > > myRequest.Method = "POST"
    > > myRequest.ContentType = "text/xml"
    > > myRequest.PreAuthenticate = True
    > > Dim networkCredential As New NetworkCredential("myName", "myPassword",
    > > "MyDomain")
    > >
    > > myRequest.Credentials = networkCredential
    > >
    > > Dim postData As String = "<test></test>"
    > > Dim encoder As New ASCIIEncoding
    > > Dim byteArray As Byte() = encoder.GetBytes(postData)
    > > myRequest.ContentLength = byteArray.Length
    > >
    > > Dim readStream As Stream = myRequest.GetRequestStream()
    > >
    > > readStream.Write(byteArray, 0, postData.Length)
    > > readStream.Close()
    > >

    >
    >
    >
     
    =?Utf-8?B?Sm9lbA==?=, Oct 21, 2005
    #3
  4. Well, the Request Stream isn't a file location either. Worse, you can't
    write to it. It's like an email that you receive. You can't write a reply to
    an email by opening the email and writing into it. You have to send an email
    reply. Request is one way. So is Response (the other way). If these files
    are in a SharePoint document library, I suggest you use the SharePoint
    services API to replace them. Otherwise, you'll be in worse trouble than you
    are now, because you'll break your SharePoint Services.

    --
    HTH,

    Kevin Spencer
    Microsoft MVP
    ..Net Developer
    Ambiguity has a certain quality to it.

    "Joel" <> wrote in message
    news:...
    >I can try but I was testing this because I'm planning to access xml files
    > stored in a sharepoint document library and the link to those isn't a file
    > location.
    >
    > "Kevin Spencer" wrote:
    >
    >> Try writing to a file location on the server instead of the RequestStream
    >> coming from the browser.
    >>
    >> --
    >> HTH,
    >>
    >> Kevin Spencer
    >> Microsoft MVP
    >> ..Net Developer
    >> Ambiguity has a certain quality to it.
    >>
    >> "Joel" <> wrote in message
    >> news:...
    >> > Hi,
    >> >
    >> > I have an XML file at http://localhost/test.xml that I want to read and
    >> > then
    >> > update.
    >> > I can get and read the file no problem with GetResponseStream etc...
    >> >
    >> > The code (see 2 examples below) I am using to write the new xml to that
    >> > same
    >> > location does not give me any error but the file never updates. When I
    >> > open
    >> > the file from XMLSPY with Open URL ... I can read and update it.
    >> >
    >> > Thanks for your help.
    >> >
    >> > First example
    >> >
    >> > Dim myRequest As HttpWebRequest =
    >> > CType(WebRequest.Create("http://localhost/test.xml"), HttpWebRequest)
    >> >
    >> > myRequest.Method = "POST"
    >> > myRequest.PreAuthenticate = True
    >> > Dim networkCredential As New NetworkCredential("myName",
    >> > "MyPassword",
    >> > "MyDomain")
    >> >
    >> > myRequest.Credentials = networkCredential
    >> >
    >> > ' this one doesn't work either myRequest.ContentType =
    >> > "application/x-www-form-urlencoded"
    >> > myRequest.ContentType = "text/xml"
    >> >
    >> > Dim newStream As Stream = myRequest.GetRequestStream()
    >> > Dim sw As New StreamWriter(newStream)
    >> > sw.Write("<test></test>")
    >> > sw.Flush()
    >> > sw.Close()
    >> >
    >> > Second example
    >> >
    >> > Dim myRequest As WebRequest =
    >> > HttpWebRequest.Create("http://localhost/test.xml")
    >> > myRequest.Method = "POST"
    >> > myRequest.ContentType = "text/xml"
    >> > myRequest.PreAuthenticate = True
    >> > Dim networkCredential As New NetworkCredential("myName",
    >> > "myPassword",
    >> > "MyDomain")
    >> >
    >> > myRequest.Credentials = networkCredential
    >> >
    >> > Dim postData As String = "<test></test>"
    >> > Dim encoder As New ASCIIEncoding
    >> > Dim byteArray As Byte() = encoder.GetBytes(postData)
    >> > myRequest.ContentLength = byteArray.Length
    >> >
    >> > Dim readStream As Stream = myRequest.GetRequestStream()
    >> >
    >> > readStream.Write(byteArray, 0, postData.Length)
    >> > readStream.Close()
    >> >

    >>
    >>
    >>
     
    Kevin Spencer, Oct 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon paugh
    Replies:
    1
    Views:
    955
  2. =?Utf-8?B?Um9ja3k=?=

    Updating XML File

    =?Utf-8?B?Um9ja3k=?=, Dec 28, 2005, in forum: ASP .Net
    Replies:
    9
    Views:
    610
    =?Utf-8?B?U2VyZ2V5IFBvYmVyZXpvdnNraXk=?=
    Jan 3, 2006
  3. news.rcn.com
    Replies:
    2
    Views:
    1,182
    Roedy Green
    Dec 10, 2007
  4. Just D.
    Replies:
    0
    Views:
    557
    Just D.
    Aug 11, 2004
  5. Erik Wasser
    Replies:
    5
    Views:
    527
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page