• <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#基礎}

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

            搜索

            •  

            積分與排名

            • 積分 - 1807508
            • 排名 - 5

            最新評論

            閱讀排行榜

            久久综合综合久久97色| 久久久九九有精品国产| 色播久久人人爽人人爽人人片aV| 99热热久久这里只有精品68| 久久久久噜噜噜亚洲熟女综合| 欧美精品乱码99久久蜜桃| av色综合久久天堂av色综合在 | 精产国品久久一二三产区区别| 亚洲伊人久久大香线蕉综合图片| 久久香综合精品久久伊人| 蜜桃麻豆www久久国产精品| 久久国产精品无码一区二区三区 | 亚洲精品tv久久久久| 精品精品国产自在久久高清| 综合久久一区二区三区| 久久精品国产一区| 亚洲∧v久久久无码精品| 要久久爱在线免费观看| 久久夜色精品国产亚洲| 久久狠狠爱亚洲综合影院| 亚洲国产精品一区二区久久| 人妻无码αv中文字幕久久琪琪布| 久久综合欧美成人| 狠狠色丁香久久婷婷综| 久久天天躁狠狠躁夜夜96流白浆| 一97日本道伊人久久综合影院| 人人狠狠综合久久亚洲婷婷| 久久精品黄AA片一区二区三区| 欧美精品九九99久久在观看| 97久久精品人人做人人爽| 久久亚洲美女精品国产精品| 蜜桃麻豆WWW久久囤产精品| 久久精品国产99久久久香蕉| 青青青青久久精品国产h| 亚洲狠狠久久综合一区77777| 久久久久久久久久久久中文字幕 | 亚洲精品tv久久久久久久久| 亚洲精品午夜国产va久久| 无码精品久久一区二区三区| 热RE99久久精品国产66热| 无码精品久久一区二区三区 |