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

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

搜索

  •  

積分與排名

  • 積分 - 1819135
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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噜噜一区| 欧美激情在线有限公司| 一区二区三区产品免费精品久久75| 国产精品午夜春色av| 久久综合色播五月| 亚洲欧美三级伦理| 亚洲黄色在线观看| 亚洲一区二区三区激情| 久久亚洲精品欧美| 久久久久久久久一区二区| 亚洲一区尤物| 欧美mv日韩mv国产网站app| 欧美在线播放高清精品| 亚洲一级一区| 亚洲少妇中出一区| 一本色道久久88亚洲综合88| 亚洲国产91精品在线观看| 美腿丝袜亚洲色图| 久久久99久久精品女同性| 亚洲一区精彩视频| 亚洲一级在线观看| 亚洲一区亚洲二区| 亚洲二区在线观看| 久久亚洲国产精品日日av夜夜| 国产精品美女主播| 国产精品亚洲网站| 在线一区二区三区四区五区| 欧美99在线视频观看| 欧美国产日韩一区二区三区| 性做久久久久久久久| 久久精品国产亚洲高清剧情介绍| 亚洲精品国产精品国自产观看| 亚洲日本一区二区| 久久综合色婷婷| 国内免费精品永久在线视频| 国产色综合网| 亚洲国产一区二区三区在线播 | 亚洲欧美视频| 欧美视频精品一区| 国产精品看片你懂得| 一区二区欧美国产| 亚洲精品网站在线播放gif| 一本色道久久综合亚洲二区三区 | 久久久久久久久久久成人| 国产精品推荐精品| 欧美一区二区在线播放| 久久精品亚洲一区二区| 久久综合九色综合网站| 亚洲高清精品中出| 欧美+亚洲+精品+三区| 亚洲精品视频免费| 亚洲欧美日韩综合aⅴ视频| 久久久91精品| 久久久久久久欧美精品| 一区在线播放| 亚洲一级在线| 亚洲一区一卡| 狠狠色狠狠色综合日日tαg| 亚洲一区二区免费在线| 亚洲在线成人精品| 国产一区二区三区奇米久涩| 在线中文字幕一区| 亚洲视频欧美视频| 国产自产2019最新不卡| 免费国产自线拍一欧美视频| 女仆av观看一区| 一区二区三区产品免费精品久久75 | 免费日韩精品中文字幕视频在线| 国产乱码精品一区二区三区五月婷 | 亚洲一二三区在线观看| 国产女主播一区| 亚洲日本无吗高清不卡| 99在线热播精品免费| 欧美成人免费观看| 国产亚洲欧美一区| 亚洲二区视频在线| 国产精品青草久久久久福利99| 狂野欧美一区| 欧美在线观看一区二区| 日韩视频在线免费观看| 女同一区二区| 国产精品每日更新| 亚洲动漫精品| 国产私拍一区| 亚洲欧洲一区二区在线播放| 国产日韩在线视频| 亚洲人成久久| 精品69视频一区二区三区| 一本久久综合亚洲鲁鲁五月天| 一区二区三区无毛| 在线一区二区三区四区| 亚洲国产一成人久久精品| 亚洲欧美激情视频在线观看一区二区三区| 欧美日本网站| 亚洲福利视频三区| 亚洲第一福利视频| 国产欧美精品在线观看| 99av国产精品欲麻豆| 欧美日韩精品一区| 亚洲夜间福利| 久久综合久久综合久久| 欧美自拍丝袜亚洲| 久久精品国产久精国产思思| 亚洲欧美日韩一区二区三区在线观看 | 欧美国产日韩一区二区| 国产精品资源| 99在线热播精品免费| 亚洲蜜桃精久久久久久久| 夜夜夜精品看看| 99日韩精品| 亚洲在线观看免费视频| 在线综合+亚洲+欧美中文字幕| 欧美成人亚洲成人日韩成人| 欧美成人午夜剧场免费观看| 欧美高清视频在线播放| 亚洲一区国产视频| 欧美日韩视频专区在线播放 | 亚洲欧洲在线一区| 99国产精品一区| 一区二区免费在线视频| 欧美极品aⅴ影院| 欧美一区二区视频97| 欧美色综合天天久久综合精品| 亚洲级视频在线观看免费1级| 亚洲国产一区二区视频| 欧美福利视频| 日韩午夜免费| 亚洲影院在线| 国产精品一区三区| 欧美在现视频| 亚洲福利国产精品| 亚洲夜晚福利在线观看| 国产精品拍天天在线| 亚洲欧美激情一区二区| 久久精精品视频| 亚洲国产精品一区二区第一页 | 国产欧美一区二区白浆黑人| 亚洲男人影院| 老司机aⅴ在线精品导航| 亚洲国产高清一区| 欧美日韩精品中文字幕| 亚洲综合色在线| 蜜桃av一区二区三区| 亚洲毛片一区| 国产精品尤物| 鲁鲁狠狠狠7777一区二区| 亚洲激情视频网站| 欧美亚洲网站| 欧美精品午夜| 午夜精品久久久| 欧美成人精品三级在线观看| 夜夜嗨av一区二区三区中文字幕| 国产精品av免费在线观看| 亚洲高清久久久| 亚洲欧美激情视频| 亚洲国产1区| 国产人久久人人人人爽| 狼人社综合社区| 亚洲伊人网站| 亚洲欧洲在线免费| 久久网站热最新地址| 亚洲精品亚洲人成人网| 国产一区二区三区奇米久涩 | 国产精品久久久久免费a∨ | 亚洲六月丁香色婷婷综合久久| 亚洲一区二区三区激情| 影音先锋一区| 免费人成网站在线观看欧美高清| 宅男66日本亚洲欧美视频 | 在线亚洲精品| 激情av一区| 国产精品国产一区二区| 亚洲尤物影院| 亚洲蜜桃精久久久久久久| 快射av在线播放一区| 欧美一级电影久久| 亚洲视频www| 国产精品一区二区久久精品| 欧美成人精品一区| 久久九九精品| 亚洲欧美激情诱惑| 中文久久精品| 日韩视频免费观看高清完整版| 欧美国产免费| 久久综合伊人| 久久男人资源视频| 亚洲精品极品| 亚洲国产精品va在线看黑人| 国产亚洲欧美激情| 国产欧美日本一区二区三区| 国产精品欧美一区二区三区奶水 | 欧美成人亚洲| 欧美成黄导航| 猫咪成人在线观看| 欧美成人69av| 欧美激情中文字幕乱码免费| 欧美成人综合网站| 欧美日韩高清在线|