• <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++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

            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 夢在天涯 閱讀(1953) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導航

            統計

            • 隨筆 - 461
            • 文章 - 4
            • 評論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1804303
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久国产精品99久久久久久老狼 | 无码日韩人妻精品久久蜜桃| 2021最新久久久视精品爱| 久久久久亚洲AV无码专区首JN| 久久综合香蕉国产蜜臀AV| 国产精品va久久久久久久| 久久夜色精品国产亚洲av| 日本强好片久久久久久AAA| 国产精品无码久久四虎| 亚洲va久久久噜噜噜久久天堂| 麻豆精品久久精品色综合| 久久综合亚洲鲁鲁五月天| 青青草国产精品久久久久| 人妻少妇久久中文字幕一区二区| 国产亚洲色婷婷久久99精品91 | 国产精品VIDEOSSEX久久发布| 久久久亚洲裙底偷窥综合| 久久精品亚洲福利| 久久国产免费观看精品| 婷婷综合久久中文字幕蜜桃三电影| 久久99精品久久久久久齐齐| 久久久精品人妻一区二区三区四| 欧美成a人片免费看久久| 香蕉久久一区二区不卡无毒影院| 97精品国产97久久久久久免费| 久久久99精品成人片中文字幕 | 91精品国产91久久综合| 亚洲中文久久精品无码ww16 | 2021国产精品午夜久久| 久久99精品久久久久久水蜜桃| 色综合久久久久网| 99久久国产热无码精品免费| 久久久噜噜噜久久熟女AA片| 777午夜精品久久av蜜臀| 久久久久久精品久久久久| 欧美国产成人久久精品| 热99RE久久精品这里都是精品免费| 久久国产AVJUST麻豆| 久久久这里只有精品加勒比| 中文国产成人精品久久不卡| 伊人久久大香线蕉综合影院首页|