我在序列化以后,得到如下数据
<?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呢?
<?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呢?
--------------------
StreamWriter writer = new StreamWriter(filePath,false,Encoding.UTF8);
XmlSerializer mySerializer = new XmlSerializer(typeof(myclass));
System.Xml.XmlDeclaration xmldeclare = (System.Xml.XmlDeclaration)doc.FirstChild;
xmldeclare.Encoding = "gb2312";
writer.Formatting = Formatting.Indented;
writer.Namespaces = false;
xs.Serialize(writer);
doc.Load(@"../../test.xml");
System.Xml.XmlDeclaration xmlDeclaration = (System.Xml.XmlDeclaration)doc.FirstChild;
xmlDeclaration.Encoding = "utf-8";
doc.Save(@"../../test.xml");
{
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>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"干嘛要去掉呢?
这两个声明,不知道会不会出现问题,所以才有此一问。