我在序列化以后,得到如下数据
<?xml version="1.0" encoding="gb2312"?>
<abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <name>aaa</name>
</abc>请问,如何去掉里面的
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
如果使用字符串查找替换的方法去掉的话,反序列化时会不会有问题呢?如何将<?xml version="1.0" encoding="gb2312"?>里面的gb2312改成utf-8呢?

解决方案 »

  1.   

    如何将<?xml version="1.0" encoding="gb2312"?>里面的gb2312改成utf-8呢?
    --------------------
    StreamWriter writer = new StreamWriter(filePath,false,Encoding.UTF8);
    XmlSerializer mySerializer = new XmlSerializer(typeof(myclass));
      

  2.   

    try..
        System.Xml.XmlDeclaration xmldeclare = (System.Xml.XmlDeclaration)doc.FirstChild;
    xmldeclare.Encoding = "gb2312";
      

  3.   

    XmlTextWriter writer = new XmlTextWriter(fileName, Encoding.UTF8);
                writer.Formatting = Formatting.Indented;
                writer.Namespaces = false;
      

  4.   

    System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(this.GetType());
                xs.Serialize(writer);
      

  5.   

    XmlDocument doc = new XmlDocument();
                doc.Load(@"../../test.xml");
                System.Xml.XmlDeclaration xmlDeclaration = (System.Xml.XmlDeclaration)doc.FirstChild;
                xmlDeclaration.Encoding = "utf-8";
                doc.Save(@"../../test.xml");
      

  6.   

    for example:class Program
        {
            static void Main(string[] args)
            {
                StreamWriter sw = new StreamWriter(@"../../student.xml", false, Encoding.UTF8);
                XmlSerializer serializer = new XmlSerializer(typeof(student));
                student st = new student();
                st.sno = "1111";
                st.sname = "test";
                st.description = "good";
                serializer.Serialize(sw, st);
            }
        }
        public class student
        {
            public string sno;
            public string sname;
            public string description;
        }
    输出:
    student.xml:<?xml version="1.0" encoding="utf-8"?>
    <student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <sno>1111</sno>
      <sname>test</sname>
      <description>good</description>
    </student>
      

  7.   

    TO:请问,如何去掉里面的
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"干嘛要去掉呢?
      

  8.   

    to:liujia_0421(SnowLover) 需要将生成的XML传递到另一个公司编写的软件中,如果包括xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    这两个声明,不知道会不会出现问题,所以才有此一问。