在xml中添加序列化对象时如何屏蔽头属性 <?xml version="1.0" encoding="UTF-8"?>

2025-01-04 05:06:16
推荐回答(1个)
回答1:

如下

                XmlTextWriter xmlWriter = new XmlTextWriter(filename,null);//不添加utf-8 BOM

                xmlWriter.Formatting = Formatting.Indented;

                // This will output the XML declaration
                xmlWriter.WriteStartDocument(true);//添加standalone="yes"
                //xmlWriter.Formatting = Formatting.None;//这句让xml失去任何缩进等格式

                xmlWriter.WriteStartElement("DocumentElement");


                    xmlWriter.WriteStartElement("Table");
                    //xmlWriter.WriteAttributeString("id", "01");//添加之后类似于                    xmlWriter.WriteElementString("Att1","value1");
                    xmlWriter.WriteElementString("Att2","value2");
                    xmlWriter.WriteEndElement();
                xmlWriter.WriteEndDocument();
                xmlWriter.Close();

这样下来就是不带utf-8的,如果不添加下面这句xmlWriter.WriteStartDocument(true);//添加standalone="yes",那么文件头只有一个xml1.0的版本申明