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

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 夢在天涯 閱讀(745) 評論(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

搜索

  •  

積分與排名

  • 積分 - 1811981
  • 排名 - 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>
              久久精品成人一区二区三区| 久久成人久久爱| 国产精品进线69影院| 欧美日韩成人网| 欧美精品在线观看一区二区| 欧美精品乱人伦久久久久久| 欧美成人激情视频| 欧美日韩国产欧| 欧美色图麻豆| 国内精品视频666| 在线精品国产欧美| 99热这里只有精品8| 午夜久久黄色| 另类亚洲自拍| av成人免费| 欧美专区一区二区三区| 亚洲欧洲视频| 欧美精品久久久久久久久老牛影院| 欧美精品1区| 欧美性大战xxxxx久久久| 国产一级揄自揄精品视频| 亚洲国产成人久久综合| 一本色道久久88综合日韩精品 | 黄色成人在线观看| 在线欧美日韩国产| 亚洲一区二区在线视频| 欧美亚洲视频在线看网址| 欧美va亚洲va国产综合| 亚洲婷婷在线| 久久只精品国产| 国产精品mv在线观看| 在线不卡a资源高清| 亚洲中字在线| 亚洲福利av| 亚洲一区www| 欧美**人妖| 国产三级精品三级| 亚洲视频专区在线| 亚洲福利免费| 久久亚洲私人国产精品va媚药| 国产精品国产三级国产普通话蜜臀 | 欧美大片免费观看在线观看网站推荐| 欧美日韩国产免费| 亚洲国产网站| 久久综合国产精品台湾中文娱乐网| 亚洲精品国久久99热| 久久av在线| 国产精品影音先锋| 亚洲午夜久久久久久久久电影网| 亚洲丁香婷深爱综合| 久久精品电影| 国产午夜精品理论片a级大结局| 一区二区三区四区国产精品| 亚洲国产精品电影在线观看| 久久精品毛片| 国产亚洲欧美激情| 欧美在线首页| 亚洲欧洲av一区二区| 国产精品大片免费观看| 一区二区三区av| 亚洲美女电影在线| 欧美黄色影院| 99精品热视频只有精品10| 欧美激情第8页| 美女国内精品自产拍在线播放| 在线精品视频一区二区三四| 国产一区二三区| 国产精品丝袜91| 亚洲免费观看高清在线观看 | 亚洲一区www| 国产精品xvideos88| 99热免费精品在线观看| 亚洲精品一区二区三区av| 欧美国产视频一区二区| 日韩一级精品| 亚洲另类在线一区| 欧美视频1区| 亚洲综合精品一区二区| 亚洲一区二区三区精品在线| 国产午夜精品全部视频播放| 久久九九国产精品怡红院| 久久黄色小说| 亚洲日本va午夜在线影院| 亚洲激情偷拍| 国产精品欧美日韩久久| 久久色在线播放| 久久一区国产| 一本久道久久综合狠狠爱| 亚洲视频精选在线| 红桃视频国产一区| 亚洲久久一区二区| 国产欧美韩国高清| 欧美高清视频| 国产精品九色蝌蚪自拍| 麻豆成人综合网| 欧美日韩中文字幕综合视频| 久久精品视频va| 欧美激情 亚洲a∨综合| 午夜日韩在线观看| 麻豆精品视频在线观看| 99国产精品久久久久久久成人热| 日韩视频免费观看| 在线观看91精品国产入口| 亚洲精品国产精品国产自| 国产亚洲aⅴaaaaaa毛片| 亚洲激情影院| 国内外成人免费激情在线视频| 亚洲免费观看高清完整版在线观看熊| 国产欧美精品va在线观看| 亚洲第一精品夜夜躁人人躁 | 久久久久久久久久码影片| 亚洲最黄网站| 久久aⅴ国产欧美74aaa| 亚洲性线免费观看视频成熟| 久久高清一区| 亚洲永久免费精品| 久久露脸国产精品| 午夜宅男久久久| 欧美日韩国产片| 欧美v日韩v国产v| 国产精品视频xxx| 亚洲激情一区二区| 在线看日韩欧美| 亚洲一区二区三区影院| 欧美1区视频| 国产日韩精品在线| 亚洲一区尤物| 亚洲视频每日更新| 欧美大片专区| 欧美成人精品影院| 激情自拍一区| 久久久不卡网国产精品一区| 欧美亚洲一区在线| 国产精品久久国产愉拍| 99pao成人国产永久免费视频| 亚洲三级免费| 欧美剧在线免费观看网站| 女人色偷偷aa久久天堂| 国产亚洲精品自拍| 久久国产精品一区二区三区四区| 午夜精品一区二区在线观看 | 久久av一区二区三区漫画| 欧美一级黄色录像| 国产精品亚洲综合一区在线观看| 99re8这里有精品热视频免费| 亚洲国产精品久久久久婷婷884| 久久久精品999| 美女啪啪无遮挡免费久久网站| 国内精品亚洲| 久久久久久久久久久久久9999| 久久婷婷久久一区二区三区| 国产亚洲午夜高清国产拍精品| 亚洲欧美久久久久一区二区三区| 亚洲欧美日韩国产精品 | 久久精品人人做人人爽| 麻豆精品传媒视频| 亚洲国产成人av好男人在线观看| 久久蜜桃香蕉精品一区二区三区| 玖玖在线精品| 亚洲精品免费在线| 欧美激情亚洲国产| 亚洲乱亚洲高清| 亚洲自拍偷拍色片视频| 国产欧美精品日韩| 久久不射网站| 欧美韩国日本综合| 中文国产成人精品| 国产伦精品一区二区三区高清| 久久精品日韩| 亚洲人成网站999久久久综合| 亚洲免费视频观看| 国产在线精品二区| 欧美高清自拍一区| 亚洲一区二区三区视频播放| 久久综合色综合88| 一区二区日本视频| 国产精品视频你懂的| 久热成人在线视频| 99re6这里只有精品| 欧美阿v一级看视频| 亚洲一区二区三区久久| 狠狠综合久久| 国产精品女人久久久久久| 久久久久久婷| 亚洲色图综合久久| 欧美国产日韩一区二区三区| 亚洲深夜福利网站| 亚洲高清三级视频| 国产女精品视频网站免费| 亚洲图片欧洲图片日韩av| 欧美一二区视频| 亚洲精品乱码| 国产视频一区二区三区在线观看| 美日韩丰满少妇在线观看| 亚洲一级特黄| 洋洋av久久久久久久一区| 免费观看亚洲视频大全| 午夜在线精品偷拍| 一区二区三区你懂的| 亚洲国产精品一区制服丝袜|