废话就不说了。麻烦大家帮我看看,急!万分感谢!
某对象序列化后XML如下:
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Field>
…………………………
…………………………对象元数据如下:
 [Serializable]
    [XmlRoot("root")]
    public class FieldDataInfo
    {……………………
……………………现在的问题是XML root根节点有“xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 的属性。
如何将这个属性去掉?
也就是如何定义对象原数据(或其他方法,如设置序列化器),让类实例序列化后的XML文件没有属性。
也就是这个样子:
<?xml version="1.0"?>
<root>
  <Field>
…………………………
…………………………万分感谢 !

解决方案 »

  1.   

    XmlSerializer mySerializer = new XmlSerializer(typeof(类));
    StreamWriter myWriter = new StreamWriter(文件路径);
    XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
    xmlns.Add(String.Empty, String.Empty);
    mySerializer.Serialize(myWriter, 类的实例, xmlns);
    myWriter.Close();
      

  2.   

    楼上的 ,赞一个!
    能不能在问个问题,分不够再加。
    某类定义如下:
        [Serializable]
        [XmlRoot("root")]
        public class FieldDataInfo
        {
            public FieldDataInfo()
            {
                Field = new List<Attribute>();
                LinkInfos = new List<LinkInfo>();
            }        public string MyArrayName; 
            
            [XmlArray("Field")]
            public List<Attribute> MyArray;        [XmlArray("Links")]
            public List<LinkInfo> LinkInfos;
        }
        
        请问如何根据这个类实例的MyArrayName值来确定MyArray字段序列化后的节点名字。
         也就是[XmlArray("Field")]中的string "Field"根据this.MyArrayName 动态决定。
      

  3.   

    想问下楼主,如何设计Schema才能自动生成List <LinkInfo>的字段,而不是LinkInfo[]的字段?
      

  4.   

    楼上的兄弟。说实话,我用序列化用得比较简单。
    我在类定义的时候就用的List <LinkInfo>,泛型。不知道你所遇见的定义为LinkInfo[]后所生成的XML文本是否有什么区别。
    我是在 VS 2008 中用的
      

  5.   

    哦,我是用xsd.exe生成的,你是用自己手写的是吧,我想只要该字段类型是IList就可以序列化和反序列化吧。
    我是觉得List <LinkInfo>比LinkInfo[]用起来方便,但xsd.exe默认是生成数组类型的字段。
      

  6.   

    不知道是不是VS 2008 才支持LIST<t>序列化哦。
    再 2005 里面没试过。
      

  7.   

    刚看到消息~~~~~你看看这篇文档是否对你有帮助
    http://msdn2.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlarray.aspx使用XmlAttributeOverrides
      

  8.   

    如果不使用 xml 的Attribute,我倒是可以提供帮助。至于IList<>范性的,是可以序列化 的http://topic.csdn.net/u/20080220/11/a736387f-34f6-4584-b3fb-142e199c1551.html 请 参考这个帖子!