• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            C++ Programmer's Cookbook

            {C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            c# 生成 xml 文件

            方法一:
            using?System;
            using?System.Xml;
            using?System.IO;
            using?System.Text;

            public?class?ReadWriteXml?
            {

            ????
            private?static?void?Main()?
            ????
            {

            ????????
            //?Create?the?file?and?writer.
            ????????FileStream?fs?=?new?FileStream("products.xml",?FileMode.Create);
            ????????XmlTextWriter?w?
            =?new?XmlTextWriter(fs,?Encoding.UTF8);

            ????????
            //?Start?the?document.
            ????????w.WriteStartDocument();
            ????????w.WriteStartElement(
            "products");

            ????????
            //?Write?a?product.
            ????????w.WriteStartElement("product");
            ????????w.WriteAttributeString(
            "id",?"1001");
            ????????w.WriteElementString(
            "productName",?"Gourmet?Coffee");
            ????????w.WriteElementString(
            "productPrice",?"0.99");
            ????????w.WriteEndElement();

            ????????
            //?Write?another?product.
            ????????w.WriteStartElement("product");
            ????????w.WriteAttributeString(
            "id",?"1002");
            ????????w.WriteElementString(
            "productName",?"Blue?China?Tea?Pot");
            ????????w.WriteElementString(
            "productPrice",?"102.99");
            ????????w.WriteEndElement();

            ????????
            //?End?the?document.
            ????????w.WriteEndElement();
            ????????w.WriteEndDocument();
            ????????w.Flush();
            ????????fs.Close();

            ????????Console.WriteLine(
            "Document?created.?"?+
            ????????????
            "Press?Enter?to?read?the?document.");
            ????????Console.ReadLine();

            ????????fs?
            =?new?FileStream("products.xml",?FileMode.Open);
            ????????XmlTextReader?r?
            =?new?XmlTextReader(fs);

            ????????
            //?Read?all?nodes.
            ????????while?(r.Read())?
            ????????
            {
            ?
            ????????????
            if?(r.NodeType?==?XmlNodeType.Element)?
            ????????????
            {

            ????????????????Console.WriteLine();
            ????????????????Console.WriteLine(
            "<"?+?r.Name?+?">");

            ????????????????
            if?(r.HasAttributes)?
            ????????????????
            {

            ????????????????????
            for?(int?i?=?0;?i?<?r.AttributeCount;?i++)?
            ????????????????????
            {
            ????????????????????????Console.WriteLine(
            "\tATTRIBUTE:?"?+
            ????????????????????????????r.GetAttribute(i));
            ????????????????????}

            ????????????????}

            ????????????}

            ????????????
            else?if?(r.NodeType?==?XmlNodeType.Text)?
            ????????????
            {
            ????????????????Console.WriteLine(
            "\tVALUE:?"?+?r.Value);
            ????????????}

            ????????}

            ????????Console.ReadLine();
            ????}

            }

            方法二:
            ?1using?System;
            ?2using?System.Xml;
            ?3
            ?4public?class?GenerateXml?
            ?5{
            ?6
            ?7????private?static?void?Main()?
            ?8????{
            ?9
            10????????//?Create?a?new,?empty?document.
            11????????XmlDocument?doc?=?new?XmlDocument();
            12????????XmlNode?docNode?=?doc.CreateXmlDeclaration("1.0",?"UTF-8",?null);
            13????????doc.AppendChild(docNode);
            14
            15????????//?Create?and?insert?a?new?element.
            16????????XmlNode?productsNode?=?doc.CreateElement("products");
            17????????doc.AppendChild(productsNode);
            18
            19????????//?Create?a?nested?element?(with?an?attribute).
            20????????XmlNode?productNode?=?doc.CreateElement("product");
            21????????XmlAttribute?productAttribute?=?doc.CreateAttribute("id");
            22????????productAttribute.Value?=?"1001";
            23????????productNode.Attributes.Append(productAttribute);
            24????????productsNode.AppendChild(productNode);
            25
            26????????//?Create?and?add?the?sub-elements?for?this?product?node
            27????????//?(with?contained?text?data).
            28????????XmlNode?nameNode?=?doc.CreateElement("productName");
            29????????nameNode.AppendChild(doc.CreateTextNode("Gourmet?Coffee"));
            30????????productNode.AppendChild(nameNode);
            31????????XmlNode?priceNode?=?doc.CreateElement("productPrice");
            32????????priceNode.AppendChild(doc.CreateTextNode("0.99"));
            33????????productNode.AppendChild(priceNode);
            34
            35????????//?Create?and?add?another?product?node.
            36????????productNode?=?doc.CreateElement("product");
            37????????productAttribute?=?doc.CreateAttribute("id");
            38????????productAttribute.Value?=?"1002";
            39????????productNode.Attributes.Append(productAttribute);
            40????????productsNode.AppendChild(productNode);
            41????????nameNode?=?doc.CreateElement("productName");
            42????????nameNode.AppendChild(doc.CreateTextNode("Blue?China?Tea?Pot"));
            43????????productNode.AppendChild(nameNode);
            44????????priceNode?=?doc.CreateElement("productPrice");
            45????????priceNode.AppendChild(doc.CreateTextNode("102.99"));
            46????????productNode.AppendChild(priceNode);
            47
            48????????//?Save?the?document?(to?the?Console?window?rather?than?a?file).
            49????????doc.Save(Console.Out);
            50????????Console.ReadLine();
            51????}

            52}

            53

            posted on 2006-03-23 17:26 夢(mèng)在天涯 閱讀(1957) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C#/.NET

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類(lèi)

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807503
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            国产成人综合久久久久久| 香蕉aa三级久久毛片| 亚洲精品美女久久久久99| 久久久久99精品成人片试看| 色综合久久最新中文字幕| 色婷婷久久久SWAG精品| 亚洲伊人久久精品影院| 久久精品国产影库免费看| 久久久久99精品成人片牛牛影视| 伊人精品久久久久7777| 2021久久精品国产99国产精品| 久久99精品国产麻豆不卡| 久久久久亚洲精品天堂| 色99久久久久高潮综合影院| 国产成人久久精品区一区二区| 思思久久好好热精品国产| 国产综合精品久久亚洲| 亚洲精品乱码久久久久久 | 亚洲人成网站999久久久综合 | 久久免费的精品国产V∧ | 国产精品美女久久久久网| 亚洲人成无码网站久久99热国产| 欧洲精品久久久av无码电影| 久久综合色之久久综合| 国产精品丝袜久久久久久不卡| 99久久99这里只有免费的精品| 亚洲国产婷婷香蕉久久久久久| 国内精品伊人久久久久网站| 91视频国产91久久久| 潮喷大喷水系列无码久久精品| 无码日韩人妻精品久久蜜桃| 波多野结衣AV无码久久一区| 伊人久久五月天| 久久夜色精品国产噜噜亚洲a| 亚洲国产成人精品91久久久| 久久久久亚洲精品无码网址| 国产午夜电影久久| 激情五月综合综合久久69| 久久久无码精品午夜| 亚洲午夜无码AV毛片久久| 久久久久久久久久久久久久 |