使用如下代码序列化一个字符串数组:string[] a = new string[] { "aa", "bbb" };
            DataContractSerializer ser = new DataContractSerializer(typeof(string[]), "root", "");
            using (MemoryStream ms = new MemoryStream())
            {
                ser.WriteObject(ms, a);
                ms.Position = 0;
                using (StreamReader reader = new StreamReader(ms))
                {
                    string sss = reader.ReadToEnd();
                }
            }
得到sss的内容是:
<root xmlns:d1p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <d1p1:string>aa</d1p1:string>
  <d1p1:string>bbb</d1p1:string>
</root>请问,如何才能把默认的命名空间去掉呢?
我希望得到的结果是:
<root xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <string>aa</string>
  <string>bbb</string>
</root>等待高手指教。 谢谢。

解决方案 »

  1.   

    <root xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
      <string>aa </string> 
      <string>bbb </string> 
    </root> 
    如果你要这个效果..感觉自己写也不复杂把.
      

  2.   

    非也, 我的这个字符串列表是一个复杂类的一个属性。 序列化的时候去掉命名空间是为了前台(C#)和后台(Java)之间消息格式的统一。
    我说的这么简单只是为了简化问题描述。 
      

  3.   

    DataContractSerializer 序列化就是这样的结果,好像没有办法去掉.但是你可以能过对序列化的结果字符串进行 Replace 操作,把命名空单替换成空就可以了.
      

  4.   

    按理说, 这个很不应该啊, 我们的JAVA后端肯定不需要包含了微软命名空间的元素信息。 这样他们解析也会出错。 也不符合跨平台的设计。
    大家再给出出主意吧。
      

  5.   

    问题已解决: 从List<string>派生一个类, 使用[CollectionDataContract(Namespace = "")]将命名空间设置为空,然后使用这个类来序列化, 就不会出现微软的命名空间了。[CollectionDataContract(Namespace = "")]
    public class DataContractStringCollection : List<string> { }