至高吴上(Alfa.wu)

一个人,一生,能坚持做好一件事情是多么的牛XX啊!!!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

摘 自: http://blog.csdn.net/fxhflower/article/details/7276820

 

可使用以下代码:
//Create our own namespaces for the output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces ();
 //Add an empty namespace and empty value
ns.Add ("", "");
 //Create the serializer
XmlSerializer slz = new XmlSerializer (someType);
 //Serialize the object with our own namespaces (notice the overload)
slz.Serialize (myXmlTextWriter, someObject, ns);


此外,在评论中还提到了去除开头的<?xml version="1.0" encoding="utf-8"?>的方法:
XmlWriterSettings settings = new XmlWriterSettings ();
 // Remove the <?xml version="1.0" encoding="utf-8"?>
settings.OmitXmlDeclaration = true;
 XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings);
 
另外,如果出现开头没有encoding="utf-8"时,应该使用:
 XmlWriterSettings settings = new XmlWriterSettings ();
settings.Encoding = Encoding.UTF8;
 XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings);

 

posted on 2014-11-30 01:24  Alfa  阅读(1860)  评论(0编辑  收藏  举报