After fiddling with Fiddler, I came up with the following solution that works. The part that seems to solve the problem indicates request.ContentLength. I don't know an easier way to get the length, so feel free to comment if you do.
XDocument doc = XDocument.Parse( @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?> <entry xmlns:d=""http://schemas.microsoft.com/ado/2007/08/dataservices"" xmlns:m=""http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"" xmlns=""http://www.w3.org/2005/Atom""> <category scheme=""http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"" term=""MetastormModel.SimpleTest"" /> <title /> <author> <name /> </author> <updated>2011-07-19T09:28:03.173654Z</updated> <id>http://localhost/odata/service.svc/SimpleTests(7)</id> <content type=""application/xml""> <m:properties> <d:ID m:type=""Edm.Int32"">7</d:ID> <d:datetimecol m:type=""Edm.DateTime"">1950-01-01T00:00:00</d:datetimecol> <d:imagecol m:type=""Edm.Binary"" m:null=""true"" /> <d:intcol m:type=""Edm.Int32"">-44</d:intcol> <d:moneycol m:type=""Edm.Decimal"">12345.0000</d:moneycol> <d:realcol m:type=""Edm.Single"">22.11</d:realcol> <d:textcol>Hello There</d:textcol> <d:varcharcol>EFGH</d:varcharcol> </m:properties> </content> </entry>"); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("http://localhost/odata/service.svc/SimpleTests(7)"); request.Method = "MERGE"; request.Accept = "application/atom+xml,application/xml"; request.Timeout = 20000; request.ContentType = "application/atom+xml"; using (MemoryStream stream = new MemoryStream()) { using (var writer = XmlWriter.Create(stream)) { doc.WriteTo(writer); writer.Close(); } request.ContentLength = stream.Length; using (var writer = XmlWriter.Create(request.GetRequestStream())) { doc.WriteTo(writer); writer.Close(); } }
Stephen Oberauer
source share