XmlWriter 创建XML文件 我的代码

1.用2个writer.WriteStartElement("“)//可以达到子节点的效果

static void Main(string[] args)
        {
            string app_path = Application.StartupPath + "\\gouwuke.xml";
       
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            using (XmlWriter writer = XmlWriter.Create(app_path, settings))
            {
                               

                // Write a Comment node.
                writer.WriteComment("这是注释");  //注释

                writer.WriteStartElement("book");                              
                //writer.WriteAttributeString("genre", "novel");    属性          
                //writer.WriteAttributeString("ISBN", "1-8630-014");  属性

                SqlConnection con = new SqlConnection(@"server=172.16.10.80;database=gouwuke_chs;uid=admin;pwd=7418;");
                SqlDataAdapter sda = new SqlDataAdapter("select ident,productname from gwkproduct where ident like 'piaoliang100%'",con);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    writer.WriteStartElement("chanpin"); //可以达到子节点的效果
                    writer.WriteElementString("ident", ds.Tables[0].Rows[i]["ident"].ToString());
                    writer.WriteElementString("productname", ds.Tables[0].Rows[i]["productname"].ToString());
                    writer.WriteEndElement();
                }
                writer.WriteEndDocument();
                writer.Flush();
                writer.Close(); 

            }


        }

 

 

 

2.static void Main(string[] args)
        {
            string app_path = Application.StartupPath + "\\gouwuke.xml";
       
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.IndentChars = ("    ");
            using (XmlWriter writer = XmlWriter.Create(app_path, settings))
            {
                // Write XML data.
                String PItext = "type=\"text/xsl\" href=\"book.xsl\"";
                writer.WriteProcessingInstruction("xml-stylesheet", PItext);

                // Write the DocumentType node.
                writer.WriteDocType("book", null, null, "<!ENTITY h \"hardcover\">");

                // Write a Comment node.
                writer.WriteComment("sample XML");  //注释

                // Write the root element.
                writer.WriteStartElement("book");

                // Write the genre attribute.
                writer.WriteAttributeString("genre", "novel");

                // Write the ISBN attribute.
                writer.WriteAttributeString("ISBN", "1-8630-014");

                // Write the title.
                writer.WriteElementString("title", "The Handmaid's Tale");

                // Write the style element.
                writer.WriteStartElement("style");
                writer.WriteEntityRef("h");
                writer.WriteEndElement();

                // Write the price.
                writer.WriteElementString("price", "19.95");

                // Write CDATA.
                writer.WriteCData("Prices 15% off!!");//<![CDATA[Prices 15% off!!]]>样式

                // Write the close tag for the root element.
                writer.WriteEndElement();

                writer.WriteEndDocument();

                // Write the XML to file and close the writer.
                writer.Flush();
                writer.Close(); 

            }


        }

 

你可能感兴趣的:(Writer)