```csharp private T DeSerialize(string xmlData, Type objectType) { using (var stringReader = new StringReader(xmlData)) { var xmlReader = XmlReader.Create(stringReader); var serializer = new XmlSerializer(objectType); return (T)serializer.Deserialize(xmlReader); } } ```
接下来,定义一个Web方法,用于接收并处理序列化的Hashtable数据:
```csharp [WebMethod(Description = "接收并处理Hashtable数据")] public bool ProcessHashtable(string xmlData) { var dictiOnaryEntries= DeSerialize(xmlData, typeof(DictionaryEntry[])); var hashtable = new Hashtable(); foreach (var entry in dictionaryEntries) { hashtable.Add(entry.Key, entry.Value); } // 这里可以添加对hashtable的进一步处理逻辑 return true; } ```
```csharp public string Serialize(T data) { using (var stringWriter = new StringWriter()) { var serializer = new XmlSerializer(data.GetType()); serializer.Serialize(stringWriter, data); return stringWriter.ToString(); } }
private void SendHashtable() { var service = new DemoService.Service1(); var hashtable = new Hashtable { { "User", "yifeng" }, { "Address", "this is yifeng's Address" }, { "Age", "20" } };
var dictiOnaryEntries= new DictionaryEntry[hashtable.Count]; hashtable.CopyTo(dictionaryEntries, 0); var xmlData = Serialize(dictionaryEntries); var success = service.ProcessHashtable(xmlData); } ```