xmldoc = new XmlDocument();
xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;这样写在文件中的是<?xml version="1.0"?>我如果想要<?xml version="1.0" encoding="utf-8"?>的结果, 应当怎么设置我找了好久没有看到, 谢谢
xmlnode = xmldoc.CreateNode ( XmlNodeType.XmlDeclaration , "" , "" ) ;这样写在文件中的是<?xml version="1.0"?>我如果想要<?xml version="1.0" encoding="utf-8"?>的结果, 应当怎么设置我找了好久没有看到, 谢谢
应该是
xmldoc.CreateXmlDeclaration("1.0",System.Text.Encoding.UTF8,"yes")
在我生成的XML里面, 看不到任何类似<?xml version="1.0"?>的东西, 也就是说开头什么东西也没有了.我对这不怎么了解,希望得到回答, 谢谢
XmlDeclaration node_dec = xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
xDoc.AppendChild(node_dec);
XmlNode node = xDoc.CreateElement("root");
xDoc.AppendChild(node);
XmlTextWriter xw = new XmlTextWriter(@"c:\1.xml", System.Text.Encoding.UTF8);
xw.Formatting = Formatting.Indented; //表示写入节点时需要断行并在行首插入空白符
xw.IndentChar = '\t'; //表示行首空白符为制表符,默认为2个空格符
xw.Indentation = 1;
xDoc.Save(xw);
xw.Close();
XmlDeclaration node_dec = xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
xDoc.AppendChild(node_dec);
XmlNode node = xDoc.CreateElement("root");
xDoc.AppendChild(node);XmlTextWriter xw = new XmlTextWriter(@"c:\1.xml", System.Text.Encoding.UTF8);
xw.Formatting = Formatting.Indented;
xw.IndentChar = '\t';
xw.Indentation = 1;
xDoc.Save(xw);
xw.Close();