日期:2014-05-20  浏览次数:20731 次

xml创建问题,着急,在线等!!

                        System.IO.FileStream   xmlFileStream   =   new   System.IO.FileStream(mXmlFileName,   System.IO.FileMode.Create);
                        ////创建Writer文本流
                        System.Xml.XmlTextWriter   xmlWriter   =   new   System.Xml.XmlTextWriter(xmlFileStream,   System.Text.Encoding.Default);
                        //////写出xml标题头

                        xmlWriter.WriteStartDocument(true);
                     
                        xmlWriter.WriteStartAttribute( "xmlns ",   null,   "http://www.w3.org/2000/10/XMLSchema ");
                        xmlWriter.WriteEndAttribute();
                        xmlWriter.WriteAttributeString( "xmlns ",   "po ",   null,   "http://contoso.com/po ");
                        xmlWriter.WriteEndAttribute();
                        xmlWriter.WriteStartElement( "DataSetProjects ",   "11 ",   "http://passport.kubao.com ");
                        MessageBox.Show( "ok ");
                        string   fileName=mXmlFileName;


看看是哪错了?老出错报错为:处于状态   Prolog   的标记   StartAttribute   将导致无效的   XML   文档。

------解决方案--------------------
这样创建头要好些吧

//Generate Xml Document
System.Xml.XmlDocument xmldoc;
System.Xml.XmlNode nRoot, nNode;


xmldoc = new XmlDocument();
//xml头
xmldoc.AppendChild(xmldoc.CreateXmlDeclaration( "1.0 ", "utf-8 ", " "));
//Root根节点
nRoot = xmldoc.CreateNode(XmlNodeType.Element, "Records ", " ");
// .........往nRoot中加入其它子节点
xmldoc.AppendChild(nRoot);
------解决方案--------------------
常用创建xml文件的两种方法:

1、XmlDocument :
XmlDocument doc = new XmlDocument();
............
doc.Save( "data.xml ");

2、XmlWriter:
XmlWriter writer = XmlWriter.Create( "output.xml ");
writer.WriteStartElement( "book ");
writer.WriteElementString( "price ", "19.95 ");
writer.WriteEndElement();
writer.Flush();