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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

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

導航

統計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1816420
  • 排名 - 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片| 国产精品美女www爽爽爽| 午夜精品偷拍| 性久久久久久久久| 激情成人亚洲| 亚洲国产精品久久久久婷婷884 | 日韩西西人体444www| 亚洲国产高清自拍| 欧美日韩免费高清| 午夜综合激情| 久久色中文字幕| 一区二区三区欧美日韩| 亚洲视频一区| 在线欧美视频| 99国产精品99久久久久久粉嫩| 国产精品激情av在线播放| 久久精品视频免费| 女女同性女同一区二区三区91| 99视频超级精品| 香蕉亚洲视频| 亚洲毛片一区二区| 亚洲永久网站| 亚洲精品自在久久| 亚洲性av在线| 亚洲激情另类| 校园春色国产精品| 日韩天堂av| 久久国产日韩| 亚洲欧美另类国产| 巨胸喷奶水www久久久免费动漫| 中日韩高清电影网| 久久精品综合一区| 一区二区三区鲁丝不卡| 久久丁香综合五月国产三级网站| 一本色道88久久加勒比精品 | 亚洲欧洲视频在线| 国产日韩欧美一区在线| 最新中文字幕一区二区三区| 国产在线拍偷自揄拍精品| 日韩一区二区精品葵司在线| 精品1区2区3区4区| 亚洲伊人色欲综合网| 99国产精品自拍| 噜噜噜久久亚洲精品国产品小说| 亚洲欧美国内爽妇网| 欧美大秀在线观看| 免费日本视频一区| 国产欧美一区二区三区在线看蜜臀| 亚洲精品一二区| 亚洲国产一区二区精品专区| 久久国产精彩视频| 久久国内精品视频| 国产精品自在在线| 亚洲视频狠狠| 亚洲一区二区免费看| 欧美日韩高清在线观看| 欧美韩日高清| 亚洲黄色影片| 免费久久99精品国产自在现线| 久久九九全国免费精品观看| 国产日韩欧美亚洲| 亚洲欧美日韩在线一区| 午夜精品久久久久久久久| 欧美亚洲成人免费| 亚洲一二三区精品| 欧美一区91| 国产午夜久久| 久久久国产一区二区三区| 久久久免费观看视频| 激情欧美一区二区| 久久一区视频| 亚洲国产婷婷综合在线精品| 亚洲激情视频在线播放| 欧美成人日韩| 99re热这里只有精品免费视频| 日韩视频―中文字幕| 欧美三级欧美一级| 亚洲午夜91| 久久亚洲精选| 亚洲动漫精品| 欧美日本免费| 亚洲欧美在线另类| 久久亚洲综合色| 亚洲日韩欧美视频一区| 欧美丝袜一区二区三区| 亚洲欧美久久久| 另类天堂视频在线观看| 亚洲卡通欧美制服中文| 国产精品国产三级国产a| 欧美一区1区三区3区公司| 免费成人网www| 99这里只有精品| 国产精品日韩高清| 久久夜色精品国产亚洲aⅴ| 亚洲欧洲美洲综合色网| 欧美一区成人| 亚洲黄色三级| 国产精品欧美激情| 久久综合九色综合欧美狠狠| 亚洲精品久久久久久久久久久久久| 亚洲一区二区视频在线| 激情校园亚洲| 欧美韩日一区二区三区| 巨胸喷奶水www久久久免费动漫| 亚洲精品国久久99热| 国产精品扒开腿爽爽爽视频| 久久久久久一区二区| 99re6这里只有精品视频在线观看| 久久久久国色av免费观看性色| 亚洲免费观看| 在线观看欧美| 国产伦精品一区二区三区视频孕妇 | 一区二区三欧美| 另类天堂av| 香蕉久久精品日日躁夜夜躁| 最新国产成人在线观看| 国产三级精品三级| 欧美午夜欧美| 欧美精品三级在线观看| 久久精品国产清高在天天线 | 欧美一区二区三区在| 亚洲精品网站在线播放gif| 国产亚洲欧美日韩一区二区| 欧美日韩一区二区精品| 欧美18av| 久久这里只有| 久久久久久九九九九| 亚洲欧美日韩在线| 一区二区欧美精品| 亚洲精品日韩在线观看| 欧美成人免费在线视频| 久久亚洲一区二区| 久久一区亚洲| 久久天堂国产精品| 久久久久久欧美| 久久久福利视频| 久久精品夜色噜噜亚洲a∨| 午夜精品美女久久久久av福利| 亚洲永久网站| 午夜国产一区| 欧美一区二区三区在线看| 亚洲午夜未删减在线观看| 一本色道久久综合精品竹菊| 亚洲美女在线观看| 亚洲色无码播放| 亚洲欧美日韩国产精品 | 国产一区二区日韩精品| 国产一区99| 在线成人激情黄色| 亚洲国产精品久久| 亚洲美女91| 亚洲一区二区三| 亚洲在线一区二区三区| 香蕉尹人综合在线观看| 性做久久久久久| 久久久久久久精| 男女精品视频| 亚洲欧洲一区二区在线观看| 亚洲另类自拍| 亚洲专区一二三| 久久国产88| 欧美高清成人| 欧美午夜激情小视频| 国产人成一区二区三区影院| 黑人一区二区三区四区五区| 亚洲激情欧美| 午夜精品久久久久久久久久久| 久久精品在线| 亚洲国产精品黑人久久久| 在线亚洲免费视频| 欧美一区2区视频在线观看 | 久久国产主播| 欧美成人性生活| 国产精品女同互慰在线看| 精品91免费| 亚洲制服少妇| 欧美成人有码| 亚洲午夜羞羞片| 免费成人高清| 国产精品永久免费在线| 亚洲破处大片| 久久大逼视频| 日韩图片一区| 久久午夜视频| 国产精品实拍| 亚洲精品国产日韩| 久久精品国产91精品亚洲| 亚洲国产成人精品女人久久久| 亚洲欧美大片| 欧美激情综合色综合啪啪| 国产亚洲网站| 亚洲午夜视频在线观看| 欧美二区在线播放| 欧美一区二区三区四区高清| 欧美日韩精品高清| 亚洲欧洲美洲综合色网| 久久人人97超碰人人澡爱香蕉|