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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級} {C#界面,C++核心算法} {設(shè)計模式} {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 夢在天涯 閱讀(1786) 評論(0)  編輯 收藏 引用 所屬分類: C#/.NET

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計

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

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811723
  • 排名 - 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>
              国产精品福利久久久| 欧美一区二区免费| 欧美区一区二| 亚洲免费黄色| 这里是久久伊人| 国产精品美女久久久久久免费| 亚洲专区在线| 欧美一区成人| 最新国产成人在线观看| 亚洲精品视频二区| 国产精品一区二区三区久久| 久久精品视频在线| 欧美国产亚洲另类动漫| 亚洲女女做受ⅹxx高潮| 欧美伊人久久久久久久久影院| 亚洲国内在线| 亚洲精品在线视频观看| 国产视频不卡| 亚洲全部视频| 国产精品一区二区女厕厕| 蜜桃久久精品一区二区| 欧美剧在线免费观看网站| 香蕉成人啪国产精品视频综合网| 欧美一区精品| 亚洲最新视频在线| 欧美中文字幕久久| 在线视频欧美一区| 久久久久国内| 亚洲网站在线看| 久久综合给合久久狠狠狠97色69| 亚洲一区二区三区免费观看| 欧美伊人久久久久久午夜久久久久 | 久久精品123| 欧美黑人在线观看| 久久精品视频99| 欧美午夜美女看片| 欧美国产日韩亚洲一区| 国产一区二区三区在线观看精品| 亚洲精品一区在线| 1024精品一区二区三区| 午夜一区二区三视频在线观看 | 国产精品激情电影| 亚洲精品中文字| 91久久精品国产91性色tv| 久久av资源网| 欧美一区二区日韩一区二区| 欧美日韩久久| 亚洲日韩中文字幕在线播放| 国产一区二区三区四区hd| 一区二区三区视频在线看| 99在线热播精品免费| 麻豆精品视频在线| 免费永久网站黄欧美| 国产欧美一区二区三区另类精品 | 免费欧美在线视频| 狼人天天伊人久久| 国产夜色精品一区二区av| 亚洲一区二区3| 亚洲午夜国产成人av电影男同| 欧美bbbxxxxx| 亚洲成人在线免费| 国产精品视频成人| 亚洲最快最全在线视频| 99精品欧美一区二区三区| 欧美黄色一级视频| 亚洲激情午夜| 夜夜嗨av一区二区三区| 欧美啪啪一区| 在线一区视频| 性色一区二区| 国外精品视频| 美国十次成人| 亚洲精品永久免费精品| 一本色道久久加勒比精品| 欧美日韩精品一区| 在线亚洲+欧美+日本专区| 午夜精品免费| 黄色在线成人| 欧美激情久久久久久| 一本到12不卡视频在线dvd| 亚洲欧美日韩中文视频| 国产日韩欧美成人| 久久最新视频| 亚洲毛片在线| 久久久91精品国产| 亚洲黑丝一区二区| 欧美日韩亚洲一区二| 亚洲欧美成人在线| 蜜臀a∨国产成人精品| 亚洲精品一区久久久久久| 欧美日韩小视频| 欧美中文在线观看国产| 91久久黄色| 久久激情久久| 一区二区免费在线观看| 国产乱人伦精品一区二区 | 欧美日韩国产免费| 性欧美8khd高清极品| 免费永久网站黄欧美| 亚洲午夜女主播在线直播| 国内精品视频在线观看| 欧美精品v日韩精品v国产精品| 亚洲免费一在线| 欧美激情1区2区3区| 亚洲欧美自拍偷拍| 亚洲黄色高清| 国产日韩欧美在线播放| 欧美成人dvd在线视频| 小黄鸭精品密入口导航| 最新国产乱人伦偷精品免费网站| 亚洲欧美在线免费观看| 亚洲精品一区二区三区樱花| 国产精品视频网址| 欧美全黄视频| 美玉足脚交一区二区三区图片| 中文一区字幕| 亚洲激情视频| 美日韩丰满少妇在线观看| 午夜精品久久久久久久久久久久| 亚洲精品欧洲精品| 尤妮丝一区二区裸体视频| 国产精品久久二区二区| 欧美精品亚洲精品| 久久野战av| 久久av一区二区三区漫画| 亚洲午夜电影| 一本一道久久综合狠狠老精东影业| 免费精品视频| 鲁大师成人一区二区三区| 欧美亚洲一区二区在线| 亚洲图片欧美午夜| 亚洲图片欧洲图片av| 亚洲免费av观看| 亚洲精品国产精品乱码不99| 在线看欧美视频| 国产精品蜜臀在线观看| 国产精品成人观看视频免费| 欧美日韩网址| 欧美午夜宅男影院| 国产精品久久久久久久午夜 | 美女福利精品视频| 免费欧美日韩| 欧美精品日韩综合在线| 欧美大秀在线观看| 欧美啪啪一区| 欧美视频日韩视频| 国产精品美女久久久免费 | 欧美理论片在线观看| 欧美另类极品videosbest最新版本 | 一区二区三区精品在线| 一本色道久久精品| 亚洲午夜伦理| 性做久久久久久久久| 欧美在线网站| 美女国产一区| 欧美日韩视频一区二区三区| 欧美性生交xxxxx久久久| 国产精品欧美久久| 国产在线一区二区三区四区 | 欧美性生交xxxxx久久久| 国产精品免费区二区三区观看| 国产精品免费看| 极品尤物久久久av免费看| 在线观看视频一区二区欧美日韩 | 欧美午夜在线| 国语自产偷拍精品视频偷 | 亚洲黄色高清| 午夜精品国产更新| 老司机一区二区| 亚洲精品欧美专区| 亚洲欧美日韩精品| 蜜臀久久99精品久久久久久9| 欧美激情视频一区二区三区不卡| 国产精品igao视频网网址不卡日韩| 国产精品永久在线| 亚洲第一偷拍| 性视频1819p久久| 欧美二区在线| 亚洲欧美日韩中文视频| 男男成人高潮片免费网站| 欧美日韩一区精品| 在线精品国产欧美| 亚洲视频1区2区| 麻豆精品在线视频| 在线综合亚洲欧美在线视频| 久久婷婷国产麻豆91天堂| 国产精品v欧美精品∨日韩| 伊人狠狠色j香婷婷综合| 亚洲欧美日韩综合国产aⅴ| 农村妇女精品| 亚洲欧美在线一区二区| 欧美巨乳在线观看| 亚洲二区在线视频| 欧美在线精品一区| 99视频日韩| 欧美国产一区在线| 亚洲第一页在线| 久久午夜视频| 午夜精品久久久久久久99水蜜桃 | 国产精品视频久久一区|