cannot reproduce your error, I copy the xml inside a textarea and submit to the server and everything is okhere is my console code to create the serialization xml file: 1. TestSer.cs:using System; using System.Collections; using System.IO; using System.Xml.Serialization;public class TestClass { public string data; }public class Run { public static void Main() { Run test = new Run(); test.SerializeObject("TestClass.xml"); test.DeSerializeObject("TestClass.xml"); } public void SerializeObject(string filename) { TextWriter writer = new StreamWriter(filename); XmlSerializer s = new XmlSerializer(typeof(TestClass)); TestClass tc = new TestClass(); tc.data = "Hello World"; s.Serialize(writer,tc); writer.Close(); } public void DeSerializeObject(string filename) { TextReader reader = new StreamReader(filename); XmlSerializer s = new XmlSerializer(typeof(TestClass)); TestClass tc = (TestClass)s.Deserialize(reader); Console.WriteLine(tc.data); reader.Close(); } } 2. TestClass.xml:<?xml version="1.0" encoding="utf-8"?> <TestClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <data>Hello World</data> </TestClass>3. TestSer.aspx:<%@ Import Namespace="System.Xml" %> <%@ Import Namespace="System.Xml.Serialization" %> <form runat=server> <asp:TextBox TextMode="Multiline" id=txt1 runat=server /> <input type=button value=submit onclick="update()"> </form><script language="C#" runat="server">public class TestClass { public string data; }void Page_Load(Object o, EventArgs e) { if (Request.ContentType.ToLower() == "text/xml") { Response.Clear(); try { XmlSerializer s = new XmlSerializer(typeof(TestClass)); TestClass tc = (TestClass)s.Deserialize(Request.InputStream); Response.Write(tc.data); } catch (Exception ex) { Response.Write(ex.Message); if (ex.InnerException != null) Response.Write(ex.InnerException.Message); } Response.End(); } } </script> <script language="javascript"> var http = new ActiveXObject("Microsoft.XMLHTTP");function update() { http.open("POST","TestSer.aspx",false); http.setRequestHeader("Content-Type","text/xml"); http.send(document.forms[0]["txt1"].value); if (http.status == 200) { alert("good:" + http.responseText) ; } else alert("error:"+ http.statusText);
XmlHttp.Open( "POST", url, false );
XmlHttp.setRequestHeader( "Content-Type","text/XML" );
XmlHttp.Send( xml );Request.InputStream是通过上面的代码送过来的,关键是里面的编码不知道如何处理,才会被Deserialize认识成<root xmlns=''>,现在我就是想知道这个编码问题该如何处理??????????
1. TestSer.cs:using System;
using System.Collections;
using System.IO;
using System.Xml.Serialization;public class TestClass
{
public string data;
}public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("TestClass.xml"); test.DeSerializeObject("TestClass.xml");
} public void SerializeObject(string filename)
{
TextWriter writer = new StreamWriter(filename);
XmlSerializer s = new XmlSerializer(typeof(TestClass));
TestClass tc = new TestClass();
tc.data = "Hello World"; s.Serialize(writer,tc);
writer.Close();
} public void DeSerializeObject(string filename)
{
TextReader reader = new StreamReader(filename);
XmlSerializer s = new XmlSerializer(typeof(TestClass));
TestClass tc = (TestClass)s.Deserialize(reader);
Console.WriteLine(tc.data);
reader.Close();
}
}
2. TestClass.xml:<?xml version="1.0" encoding="utf-8"?>
<TestClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<data>Hello World</data>
</TestClass>3. TestSer.aspx:<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Serialization" %>
<form runat=server>
<asp:TextBox TextMode="Multiline" id=txt1 runat=server />
<input type=button value=submit onclick="update()">
</form><script language="C#" runat="server">public class TestClass
{
public string data;
}void Page_Load(Object o, EventArgs e)
{
if (Request.ContentType.ToLower() == "text/xml")
{
Response.Clear();
try
{
XmlSerializer s = new XmlSerializer(typeof(TestClass)); TestClass tc = (TestClass)s.Deserialize(Request.InputStream);
Response.Write(tc.data);
}
catch (Exception ex)
{
Response.Write(ex.Message);
if (ex.InnerException != null)
Response.Write(ex.InnerException.Message);
} Response.End();
}
}
</script>
<script language="javascript">
var http = new ActiveXObject("Microsoft.XMLHTTP");function update()
{
http.open("POST","TestSer.aspx",false);
http.setRequestHeader("Content-Type","text/xml");
http.send(document.forms[0]["txt1"].value);
if (http.status == 200)
{
alert("good:" + http.responseText) ;
}
else
alert("error:"+ http.statusText);
}</script>