SIlverlight:将序列化的XML作为HTTP Post发送

时间:2014-07-28 07:52:40

标签: silverlight serialization kml

我正在尝试通过HTTP Post将一个KML文件(即XML)发送到ASPX页面。只要KML中没有包含转义的HTML,这就可以正常工作。

要创建KML,我将以下类用于XmlSerializer

public class kml
{
    [DataMember]
    public DocumentItem Document
    { get; set; }

    public kml()
    {
    }

    public class BalloonStyleItem
    {
        [DataMember]
        public string text
        { get; set; }
        [DataMember]
        public string displayMode
        { get; set; }

        public BalloonStyleItem()
        {
            displayMode = "default";
        }
    }
}
   //(...)

在我的代码中,创建了KML我有这一行:

balloonStyleItem.text = "&lt;h2&gt;name: " + substation.name + "&lt;/h2&gt;region: " + substation.Region.Region.name + "<br>subregion: " + substation.Region.name;

如您所见,它包含HTML

现在我想通过HTTP Post发送这个KML:

HttpWebRequest request = asyncResult.AsyncState as HttpWebRequest;
Stream stream = request.EndGetRequestStream(asyncResult);
StreamWriter writer = new StreamWriter(stream);

System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(kml));
xmlSerializer.Serialize(writer, kmlContainer);
writer.Write(xmlstring); //here it crashes!

此时程序的执行停止,并显示错误消息“WebException未被用户代码取消,远程服务器返回错误:NotFound”

如果我给出类似

的内容
balloonStyleItem.text = "xxxxx";

(没有HTML)一切正常。你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试使用DataContractSerializer而不是XmlSerializer吗?我注意到你使用的是为DataContractSerializer设计的属性......

DataContractSerializer ser = new DataContractSerializer(typeof(kml));
ser.WriteObject(stream, kmlContainer);