• <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++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

            Writing XML File using XmlWriter//Reading XML Files//Reading XML File using XmlDocument//Inserting Data to an XML Document

            The XmlWriter and XmlTextWriter classes are defined in the System.XML namespace.

            The XmlTextWriter class is derived from XmlWriter class, which represents a writer that provides fast non-cached forward-only way of generating XML documents based on  the W3C Extensible Markup Language (XML) 1.0 specification.

            In this article, I will show you how to use XmlTextWriter class to create an XML document and write data to the document.

            Adding namespace Reference

            Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.

            using System.Xml;

            Creating an XML Document

            The constructor of the XmlTextWriter class creates an XML file if file doesn't exist. In this sample, I create a new XML file called xmltest.xml in C\temp directory.

            XmlTextWriter writer = new XmlTextWriter("C:\\temp\\xmltest.xml", null);

            NOTE: If you don't want to write data in an XML file and want to display XML contents on the Console, pass Console.Out as a parameter of the constructor.

            XmlTextWriter writer = new XmlTextWriter(Console.Out);

            Adding Data to the Document

            The WriteStartDocument method starts a new document. The WriteStartElement and the WriteEndElement pair is used to add a new element to the document. The WriteString writes a string to the document.

            writer.WriteStartDocument();
            writer.WriteComment("Commentss: XmlWriter Test Program");
            writer.WriteProcessingInstruction("Instruction","Person Record");
            writer.WriteStartElement("p", "person", "urn:person");
            writer.WriteStartElement("LastName","");
            writer.WriteString("Chand");
            writer.WriteEndElement();
            writer.WriteElementInt16("age","", 25);
            writer.WriteEndDocument();

            Souce Code:   Attachment createxml1.cs 2 KB

            namespace WriteToXML
            {
            using System;
            using System.Xml;
            /// <summary>

            ///
            Summary description for Class1.
            /// </summary>

            public class Class1
            {
            public Class1()
            {
            }
            public static int Main(string[] args)
            {
            try
            {
            // Creates an XML file is not exist
            XmlTextWriter writer = new XmlTextWriter("C:\\temp\\xmltest.xml", null);
            // Starts a new document
            writer.WriteStartDocument();
            //Write comments
            writer.WriteComment("Commentss: XmlWriter Test Program");
            writer.WriteProcessingInstruction("Instruction","Person Record");
            // Add elements to the file
            writer.WriteStartElement("p", "person", "urn:person");
            writer.WriteStartElement("LastName","");
            writer.WriteString("Chand");
            writer.WriteEndElement();
            writer.WriteStartElement("FirstName","");
            writer.WriteString("Mahesh");
            writer.WriteEndElement();
            writer.WriteElementInt16("age","", 25);
            // Ends the document
            writer.WriteEndDocument();
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());
            }
            return 0;
            }
            }
            }

            -----------------------------------------------------------------------------------------------------------------------

            The XmlReader and XmlTextReader classes are defined in the System.XML namespace.

            The XmlTextReader class is derived from XmlReader class. The XmlTextReader class can be used to read the XML documents. The read function of this document reads the document until end of its nodes.

            In this article, I will show you how to use XmlTextReader class to read an XML document and write data to the console.

            Adding namspace Reference

            Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.

            using System.Xml;

            Open an XML Document

            The constructor of the XmlTextReader class opens an XML file. In this sample, I used an XML file called xmltest.xml in C\temp directory. You can download the attached file.

            // Open an XML file

            XmlTextReader reader =
            new XmlTextReader("C:\\temp\\xmltest.xml");

            Reading  Data

            The Read method of the XmlTextReader class reads the data.

            while ( reader.Read() )
            {
            Console.WriteLine(reader.Name);
            }

            Souce Code:   Attachment readxml1.cs 1 KB,  xmltest.xml 1 KB

            namespace ReadXML
            {
            using System;
            using System.Xml;
            /// <summary>

            ///
            Summary description for Class1.
            /// </summary>

            public class Class1
            {
            public Class1()
            {
            }
            public static int Main(string[] args)
            {
            try
            {
            // Open an XML file
            XmlTextReader reader = new XmlTextReader("C:\\temp\\xmltest.xml");
            while ( reader.Read() )
            {
            Console.WriteLine(reader.Name);
            }
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());
            }
            return 0;
            }
            }
            }

            ------------------------------------------------------------------------------------------------------------------

            Suppose I have following XML fragment:

             <Authors>
            <Author>
            <FirstName>John</FirstName>
             <LastName>Doe</LastName>
             </Author>
             <Author>
             <FirstName>Jane</FirstName>
             <LastName>Eod</LastName>
             </Author>
             </Authors>

             Now, how can I loop through my collection of authors and for each author  retrieve its first and last name and put them in a variable strFirst and  strLast?

             - - - XMLApp.cs

            using System;
            using System.Xml;
            public class XMLApp
            {
            public void YourMethod( String strFirst, String strLast)
            {
            // Do something with strFirst and strLast.
            // ...
            Console.WriteLine( "{0}, {1}", strLast, strFirst);
            }
            public void ProcessXML( String xmlText)
            {
            XmlDocument _doc =
            new XmlDocument( );
            _doc.LoadXml( xmlText);
            // alternately, _doc.Load( _strFilename); to read from a file.
            XmlNodeList _fnames = _doc.GetElementsByTagName( "FirstName" );
            XmlNodeList _lnames = _doc.GetElementsByTagName( "LastName" );
            // I'm assuming every FirstName has a LastName in this example, your requirements may vary. //
            for ( int _i = 0; _i < _fnames.Count; ++_i )
            {
            YourMethod( _fnames[ _i].InnerText,
            _lnames[ _i].InnerText );
            }
            public static void Main( String[] args)
            {
            XMLApp _app =
            new XMLApp( );
            // Passing XML text as a String, you can also use the
            // XMLDocument::Load( ) method to read the XML from a file.
            //
            _app.ProcessXML( @" <Authors>
            <Author>
            <FirstName>John</FirstName>
            <LastName>Doe</LastName>
            </Author>
            <Author>
            <FirstName>Jane</FirstName>
            <LastName>Eod</LastName>
            </Author>
            </Authors> " );
            }
            }
            // end XMLApp


            - - - XMLApp.cs

             Remember to /reference the System.Xml.dll on the command-line  to build XMLApp.cs:
             csc.exe /r:System.Xml.dll XMLApp.cs

            --------------------------------------------------------------------------------------------------------------------------------

            The XmlNode and the XmlDocument classes can be used to insert XML data to an existing document or to a new document.

            Adding namspace Reference

            Since Xml classes are defined in the System.XML namespace, so first thing you need to do is to Add the System.XML reference to the project.

            using System.Xml;

            Loading XML to Document

            LoadXml method of XmlDocument can be used to load XML data to a document or to load an existing XML document..

            // Load XML data to a document
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<XMLFile>" +
            " <SomeData>Old Data</SomeData>" +
            "</XMLFile>");


            Inserting XML Data

            The below code inserts XML data to the file and saves file as InsertedDoc.xml.

            Souce Code:  

            try
            {
            XmlNode currNode;
            XmlDocument doc =
            new XmlDocument();
            doc.LoadXml("<XMLFile>" +
            " <SomeData>Old Data</SomeData>" +
            "</XMLFile>");
            XmlDocumentFragment docFrag = doc.CreateDocumentFragment();
            docFrag.InnerXml="<Inserted>" +
            " <NewData>Inserted Data</NewData>" +
            "</Inserted>";
            // insert the availability node into the document
            currNode = doc.DocumentElement.FirstChild;
            currNode.InsertAfter(docFrag, currNode.LastChild);
            //save the output to a file
            doc.Save("InsertedDoc.xml");
            }
            catch (Exception e)
            {
            Console.WriteLine ("Exception: {0}", e.ToString());

             output of the code looks like this -

            <XMLFile> 
            <SomeData>
              Old Data 
            <Inserted> 
            <NewData>Inserted Data</NewData>
              </Inserted>
              </SomeData>
              </XMLFile> 


             

            posted on 2005-11-22 17:54 夢(mèng)在天涯 閱讀(1779) 評(píng)論(0)  編輯 收藏 引用 所屬分類: C#/.NET

            公告

            EMail:itech001#126.com

            導(dǎo)航

            統(tǒng)計(jì)

            • 隨筆 - 461
            • 文章 - 4
            • 評(píng)論 - 746
            • 引用 - 0

            常用鏈接

            隨筆分類

            隨筆檔案

            收藏夾

            Blogs

            c#(csharp)

            C++(cpp)

            Enlish

            Forums(bbs)

            My self

            Often go

            Useful Webs

            Xml/Uml/html

            搜索

            •  

            積分與排名

            • 積分 - 1807518
            • 排名 - 5

            最新評(píng)論

            閱讀排行榜

            亚洲国产精品无码久久一区二区 | 久久精品国产亚洲AV电影| 思思久久精品在热线热| 国产三级久久久精品麻豆三级| 精品国产乱码久久久久久1区2区 | 亚洲国产精品无码久久| 日产精品久久久久久久| 国产精品成人精品久久久| 国产精品99久久久久久宅男小说| 久久国产精品99精品国产| 女同久久| 99久久国产综合精品五月天喷水| 久久亚洲sm情趣捆绑调教| 777久久精品一区二区三区无码| 思思久久99热免费精品6| 国产91色综合久久免费| 99久久精品国产一区二区| 精品99久久aaa一级毛片| 久久国产精品无码HDAV| 久久久久亚洲AV无码观看| 久久久久99精品成人片三人毛片| 97久久超碰国产精品旧版| 久久亚洲AV无码精品色午夜麻豆| 国产毛片久久久久久国产毛片 | 国产免费福利体检区久久| 97久久国产亚洲精品超碰热| 无码AV中文字幕久久专区| 免费精品国产日韩热久久| 久久九九久精品国产免费直播| 精品免费tv久久久久久久| 人妻丰满AV无码久久不卡| 18禁黄久久久AAA片| 欧美黑人激情性久久| 伊人久久大香线蕉综合热线| 色欲综合久久躁天天躁| 无码乱码观看精品久久| 欧美久久久久久| 一本色道久久99一综合| 色综合久久无码五十路人妻 | 亚洲国产天堂久久综合网站 | 久久精品免费全国观看国产|