青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

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

read and write XMl in 2 means (XML一)

Read and Write XML Without Loading an Entire Document into Memory

Solution

To write XML, create an XmlTextWriter that wraps a stream and use Write methods (such as WriteStartElement and WriteEndElement). To read XML, create an XmlTextReader that wraps a stream and call Read to move from node to node.


To write XML to any stream, you can use the streamlined XmlTextWriter. It provides Write methods that write one node at a time. These include

  • WriteStartDocument, which writes the document prologue, and WriteEndDocument, which closes any open elements at the end of the document.

  • WriteStartElement, which writes an opening tag for the element you specify. You can then add more elements nested inside this element, or you can call WriteEndElement to write the closing tag.

  • WriteElementString, which writes an entire element, with an opening tag, a closing tag, and text content.

  • WriteAttributeString, which writes an entire attribute for the nearest open element, with a name and value.

To read the XML, you use the Read method of the XmlTextReader. This method advances the reader to the next node, and returns true. If no more nodes can be found, it returns false. You can retrieve information about the current node through XmlTextReader properties, including its Name, Value, and NodeType.


EX:

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();
    }
}

-----------------------------------------------------------
Insert Nodes in an XML Document





Solution

Create the node using the appropriate XmlDocument method (such as CreateElement, CreateAttribute,
CreateNode, and so on). Then insert it using the appropriate XmlNode method (such as InsertAfter,
InsertBefore, or AppendChild).


The following example demonstrates this technique by programmatically creating a new XML document.

using System;
using System.Xml;

public class GenerateXml {

    private static void Main() {

        // Create a new, empty document.
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);

        // Create and insert a new element.
        XmlNode productsNode = doc.CreateElement("products");
        doc.AppendChild(productsNode);

        // Create a nested element (with an attribute).
        XmlNode productNode = doc.CreateElement("product");
        XmlAttribute productAttribute = doc.CreateAttribute("id");
        productAttribute.Value = "1001";
        productNode.Attributes.Append(productAttribute);
        productsNode.AppendChild(productNode);

        // Create and add the sub-elements for this product node
        // (with contained text data).
        XmlNode nameNode = doc.CreateElement("productName");
        nameNode.AppendChild(doc.CreateTextNode("Gourmet Coffee"));
        productNode.AppendChild(nameNode);
        XmlNode priceNode = doc.CreateElement("productPrice");
        priceNode.AppendChild(doc.CreateTextNode("0.99"));
        productNode.AppendChild(priceNode);

        // Create and add another product node.
        productNode = doc.CreateElement("product");
        productAttribute = doc.CreateAttribute("id");
        productAttribute.Value = "1002";
        productNode.Attributes.Append(productAttribute);
        productsNode.AppendChild(productNode);
        nameNode = doc.CreateElement("productName");
        nameNode.AppendChild(doc.CreateTextNode("Blue China Tea Pot"));
        productNode.AppendChild(nameNode);
        priceNode = doc.CreateElement("productPrice");
        priceNode.AppendChild(doc.CreateTextNode("102.99"));
        productNode.AppendChild(priceNode);

        // Save the document (to the Console window rather than a file).
        doc.Save(Console.Out);
        Console.ReadLine();
    }
}

The generated document looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<products>
  <product id="1001">
    <productName>Gourmet Coffee</productName>
    <productPrice>0.99</productPrice>
  </product>
  <product id="1002">
    <productName>Blue China Tea Pot</productName>
    <productPrice>102.99</productPrice>
  </product>
</products>
-----------------------------------------------------------
------
Quickly Append Nodes in an XML Document

Solution

Create a helper function that accepts a tag name and content and can generate the entire element
at once. Alternatively, use the XmlDocument.CloneNode method to copy branches of an XmlDocument.


Here's an example of one such helper class:

using System;
using System.Xml;

public class XmlHelper {

    public static XmlNode AddElement(string tagName, 
      string textContent, XmlNode parent) {

        XmlNode node = parent.OwnerDocument.CreateElement(tagName);
        parent.AppendChild(node);

        if (textContent != null) {

            XmlNode content;
            content = parent.OwnerDocument.CreateTextNode(textContent);
            node.AppendChild(content);
        }
        return node;
    }

    public static XmlNode AddAttribute(string attributeName,
      string textContent, XmlNode parent) {

        XmlAttribute attribute;
        attribute = parent.OwnerDocument.CreateAttribute(attributeName);
        attribute.Value = textContent;
        parent.Attributes.Append(attribute);

        return attribute;
    }
}

You can now condense the XML-generating code from recipe 5.2 with the simpler syntax shown here:

public class GenerateXml {

    private static void Main() {

        // Create the basic document.
        XmlDocument doc = new XmlDocument();
        XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
        doc.AppendChild(docNode);
        XmlNode products = doc.CreateElement("products");
        doc.AppendChild(products);

        // Add two products.
        XmlNode product = XmlHelper.AddElement("product", null, products);
        XmlHelper.AddAttribute("id", "1001", product);
        XmlHelper.AddElement("productName", "Gourmet Coffee", product);
        XmlHelper.AddElement("productPrice", "0.99", product);

        product = XmlHelper.AddElement("product", null, products);
        XmlHelper.AddAttribute("id", "1002", product);
        XmlHelper.AddElement("productName", "Blue China Tea Pot", product);
        XmlHelper.AddElement("productPrice", "102.99", product);

        // Save the document (to the Console window rather than a file).
        doc.Save(Console.Out);
        Console.ReadLine();
    }
}

Alternatively, you might want to take the helper methods such as AddAttribute and AddElement and
make them instance methods in a custom class you derive from XmlDocument.

Another approach to simplifying writing XML is to duplicate nodes using the XmlNode.CloneNode
method. CloneNode accepts a Boolean deep parameter. If you supply true, CloneNode will duplicate
the entire branch, with all nested nodes.

Here's an example that creates a new product node by copying the first node.

// (Add first product node.)

// Create a new element based on an existing product.
product = product.CloneNode(true);

// Modify the node data.
product.Attributes[0].Value = "1002";
product.ChildNodes[0].ChildNodes[0].Value = "Blue China Tea Pot";
product.ChildNodes[1].ChildNodes[0].Value = "102.99";

// Add the new element.
products.AppendChild(product);

Notice that in this case, certain assumptions are being made about the existing nodes (for example,
that the first child in the item node is always the name, and the second child is always the
price). If this assumption isn't guaranteed to be true, you might need to examine the node name
programmatically.










posted on 2005-11-23 18:09 夢在天涯 閱讀(750) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816479
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              亚洲国产一区二区在线| 欧美日韩国产综合久久| 亚洲视频一区二区在线观看 | 亚洲图片欧美午夜| 亚洲激情小视频| 久久激情久久| 欧美一区二区精品在线| 欧美日韩精品免费| 亚洲二区精品| 最新亚洲电影| 久久一区二区精品| 美女主播一区| 国外成人在线视频| 西西裸体人体做爰大胆久久久| 韩国成人理伦片免费播放| 欧美日韩一区免费| 久久久不卡网国产精品一区| 香港成人在线视频| 亚洲免费在线视频| 欧美色一级片| 夜夜嗨av一区二区三区网站四季av| 亚洲国产美女| 久久综合久久综合九色| 免费在线一区二区| 亚洲高清在线观看| 久久综合九色欧美综合狠狠| 免费黄网站欧美| 亚洲风情亚aⅴ在线发布| 久热精品在线视频| 蜜臀a∨国产成人精品| 黑丝一区二区三区| 久久一日本道色综合久久| 免费日韩av片| 亚洲精品视频啊美女在线直播| 欧美国产日韩精品| 亚洲人精品午夜| 亚洲天堂偷拍| 国产九九精品| 久久久久国产一区二区三区| 免费日本视频一区| 夜夜嗨av一区二区三区网页| 欧美日韩中文字幕精品| 亚洲视频在线一区| 久久另类ts人妖一区二区| 狠狠久久五月精品中文字幕| 老妇喷水一区二区三区| 亚洲精品国产拍免费91在线| 亚洲一区激情| 国产亚洲午夜| 欧美成人精品影院| 亚洲一区久久| 免费在线成人| 亚洲亚洲精品在线观看| 国产欧美日韩一区| 欧美成人免费va影院高清| 99视频一区二区三区| 欧美一区二视频| 亚洲国产一成人久久精品| 欧美网站在线观看| 久久成人av少妇免费| 最新国产拍偷乱拍精品| 欧美专区在线播放| 亚洲精品免费网站| 国产亚洲一区二区在线观看| 免费精品视频| 欧美怡红院视频一区二区三区| 欧美第一黄网免费网站| 亚洲一区二区三区免费观看| 在线成人欧美| 国产精品免费区二区三区观看| 久久久久九九视频| 亚洲午夜激情网页| 欧美激情久久久久| 久久久久久久一区二区| 亚洲视频中文| 亚洲精品欧洲| 激情小说另类小说亚洲欧美| 国产精品美女一区二区在线观看| 久久亚洲春色中文字幕| 亚洲免费视频网站| 日韩亚洲欧美在线观看| 欧美国产三区| 久久综合九色综合欧美就去吻| 亚洲综合色丁香婷婷六月图片| 亚洲国产欧美一区二区三区久久| 国产日韩欧美一区二区三区在线观看 | 91久久亚洲| 麻豆精品视频在线观看视频| 性色av一区二区三区在线观看| 99riav1国产精品视频| 激情久久综合| 国产婷婷色一区二区三区四区| 国产精品啊啊啊| 欧美精品一区二区三区一线天视频 | 免费视频一区| 老色批av在线精品| 久久久久国色av免费看影院 | 久久精品一本| 欧美在线高清视频| 亚洲欧洲av一区二区| 一区二区电影免费观看| 亚洲人成网站在线观看播放| 欧美高清视频在线播放| 女人天堂亚洲aⅴ在线观看| 久久躁日日躁aaaaxxxx| 久久久久亚洲综合| 久久亚洲一区| 久久综合激情| 裸体一区二区三区| 免费在线视频一区| 欧美11—12娇小xxxx| 欧美激情国产高清| 欧美激情精品| 亚洲精品久久嫩草网站秘色| 亚洲日本国产| 一本久道综合久久精品| 一区二区三区久久久| 亚洲一级一区| 欧美一区二区私人影院日本| 久久国产成人| 久久综合给合久久狠狠色| 蘑菇福利视频一区播放| 欧美精品在欧美一区二区少妇| 欧美日韩国产精品专区| 国产精品多人| 国产主播喷水一区二区| 91久久精品国产91性色| 一区二区高清视频| 午夜在线不卡| 欧美1级日本1级| 亚洲精品资源| 欧美在线亚洲综合一区| 蜜臀91精品一区二区三区| 蜜臀久久99精品久久久久久9 | 欧美三区视频| 国产欧美日韩视频在线观看| 狠色狠色综合久久| 亚洲欧洲精品一区| 午夜精品网站| 亚洲成在人线av| 在线综合亚洲| 久久女同精品一区二区| 欧美精品综合| 国产午夜精品全部视频在线播放| 在线观看日韩av电影| 中文国产成人精品久久一| 久久精品免费电影| 亚洲精选在线观看| 午夜免费在线观看精品视频| 欧美 日韩 国产在线| 国产精品永久在线| 亚洲毛片视频| 久久九九免费视频| 99视频日韩| 欧美成人精品一区二区三区| 国产欧美日本| 中日韩美女免费视频网址在线观看| 久久久亚洲国产天美传媒修理工| 亚洲黄网站黄| 久久久99精品免费观看不卡| 欧美三级日韩三级国产三级| 在线观看亚洲专区| 欧美中文字幕视频| 99精品视频一区| 免费一区视频| 在线欧美日韩| 久久在线观看视频| 午夜在线视频一区二区区别 | 国产欧美91| 亚洲一区二区三区色| 欧美激情女人20p| 久久精品综合网| 国产一区二区三区精品久久久| 亚洲在线观看免费视频| 亚洲精品国产系列| 蜜臀av性久久久久蜜臀aⅴ四虎| 国产性做久久久久久| 亚洲一区激情| 欧美一区二区三区啪啪| 羞羞答答国产精品www一本| 欧美电影免费观看网站| 一区二区三区在线免费观看| 欧美一区91| 午夜国产精品视频免费体验区| 欧美日韩一区二区三区在线视频| 亚洲看片免费| 欧美激情视频一区二区三区不卡| 久久久久一区二区| 黄色成人小视频| 久久综合导航| 久久深夜福利| 亚洲成色精品| 欧美黄免费看| 欧美激情精品| 99精品国产99久久久久久福利| 亚洲国产精品www| 欧美巨乳波霸| 亚洲新中文字幕| 亚洲欧美日韩在线观看a三区| 国产欧美成人|