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

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>
              久久久久久色| 在线视频一区二区| 久久久久久综合| 在线观看视频日韩| 欧美成人精品高清在线播放| 免费日韩一区二区| 一本色道久久99精品综合 | 欧美在线亚洲在线| 欧美一级淫片aaaaaaa视频| 激情丁香综合| 亚洲精品日产精品乱码不卡| 国产精品成人一区二区艾草| 久久久久久久久综合| 欧美1区2区3区| 亚洲女爱视频在线| 久久精品欧洲| 一区二区三区四区五区视频 | 亚洲一区二区高清| 国产亚洲人成a一在线v站 | 99精品热视频只有精品10| 一区二区三区四区国产| 国内精品写真在线观看| 亚洲欧洲日韩综合二区| 国产精品色一区二区三区| 欧美电影在线播放| 国产精品盗摄一区二区三区| 免费在线成人av| 国产精品高潮呻吟| 欧美国产日韩一区二区在线观看 | 久久影音先锋| 午夜精品一区二区三区在线视 | 国产精品jizz在线观看美国 | 一区二区免费在线观看| 欧美在线视频网站| 亚洲午夜av电影| 免费成人性网站| 久久久噜噜噜久久| 国产精品v一区二区三区 | 亚洲欧美另类综合偷拍| 蜜桃av综合| 久久综合伊人77777麻豆| 欧美性jizz18性欧美| 欧美激情视频在线播放| 精品96久久久久久中文字幕无| 日韩亚洲国产精品| 亚洲欧洲精品天堂一级| 久久久久一区| 久久精品伊人| 国产日韩精品一区二区| 一区二区三区欧美成人| 一区二区久久| 欧美日韩国产va另类| 亚洲国产福利在线| 在线观看福利一区| 久久久国产精品亚洲一区| 久久精品国产v日韩v亚洲| 国产精品久久久久高潮| 一区二区三区成人精品| 亚洲视频在线观看免费| 欧美日韩在线播放一区| av成人黄色| 亚洲女优在线| 国产精品手机视频| 亚洲欧美日韩精品在线| 羞羞答答国产精品www一本| 国产精品va在线播放我和闺蜜| 99国产精品久久久久久久久久| 亚洲午夜国产成人av电影男同| 欧美日韩综合视频| 中日韩视频在线观看| 亚洲女人天堂成人av在线| 国产精品久久久久天堂| 亚洲视频日本| 久久人人97超碰精品888 | 狂野欧美激情性xxxx欧美| 国产亚洲欧美激情| 久久精品论坛| 亚洲国产成人一区| 亚洲午夜日本在线观看| 国产精品久久| 欧美在线视频观看免费网站| 欧美福利影院| 亚洲视频在线一区观看| 国产欧美 在线欧美| 久久久综合网站| 亚洲欧洲视频在线| 午夜激情综合网| 亚洲国产精品悠悠久久琪琪| 日韩视频免费| 国产精品一区二区在线观看| 久久久久久亚洲精品杨幂换脸 | 久久久亚洲影院你懂的| 亚洲人成艺术| 久久爱www| 亚洲日本激情| 国产精品黄色| 蜜臀a∨国产成人精品| 国产精品99久久久久久久女警| 久久久精品国产免费观看同学 | 国产美女扒开尿口久久久| 久久夜色精品国产噜噜av| 一本大道久久a久久精品综合| 欧美在线观看网站| 日韩一区二区久久| 国产一区二区三区高清播放| 欧美日韩国产va另类| 久久久久久电影| 亚洲图中文字幕| 欧美激情影院| 久久久久国产精品人| 亚洲一区二区免费| 亚洲国产成人午夜在线一区| 国产精品欧美一区二区三区奶水| 欧美 亚欧 日韩视频在线| 亚洲欧美综合网| 99国产精品99久久久久久粉嫩| 久久久精品视频成人| 亚洲一区在线免费| 亚洲乱码国产乱码精品精可以看| 国产婷婷色一区二区三区四区| 欧美精品一区二区蜜臀亚洲| 久久先锋影音| 久久国产视频网站| 亚洲在线中文字幕| 一区二区免费在线视频| 亚洲人成欧美中文字幕| 免费亚洲电影在线| 久久久久久久精| 久久国产精品色婷婷| 亚洲欧美另类在线| 亚洲永久免费| 亚洲综合视频网| 亚洲一区在线直播| 亚洲一区二区三区高清不卡| 一区二区欧美激情| 一区二区三区偷拍| 一本色道88久久加勒比精品 | 麻豆av一区二区三区久久| 久久久久天天天天| 久久精品一区二区三区四区| 欧美中文字幕精品| 久久高清国产| 久久亚洲欧美| 欧美国产视频一区二区| 欧美福利一区| 亚洲精品国产精品国产自| 亚洲三级观看| 在线视频中文亚洲| 亚洲免费网址| 久久国产免费| 久久这里只有精品视频首页| 久久亚洲私人国产精品va| 久热综合在线亚洲精品| 欧美高清在线精品一区| 欧美视频手机在线| 国产精品日韩专区| 国产视频久久久久| 亚洲二区在线观看| 亚洲精品九九| 亚洲免费一在线| 久久久精品国产免大香伊| 免费看的黄色欧美网站| 亚洲精品视频一区二区三区| 一区二区三区久久| 校园激情久久| 欧美大片在线看免费观看| 欧美日韩一区二区在线观看| 国产无一区二区| 亚洲黄色免费网站| 亚洲综合三区| 噜噜噜在线观看免费视频日韩| 亚洲国产日韩欧美在线动漫| 国产精品99久久久久久久久久久久| 欧美一区二区精品久久911| 蜜臀久久99精品久久久画质超高清 | 久久精品国产一区二区电影| 欧美黑人在线观看| 亚洲图片欧洲图片日韩av| 久久久精品一区| 欧美日韩在线播放三区| 精品动漫av| 亚洲欧美视频| 欧美激情精品久久久| 9色国产精品| 久久久久久久久久久久久女国产乱| 欧美日本高清一区| 国产一区二区丝袜高跟鞋图片 | 亚洲精品国产精品国产自| 午夜精品福利视频| 亚洲国产精品一区二区久 | 亚洲天堂偷拍| 麻豆国产精品va在线观看不卡 | 欧美日韩成人一区二区| 黑人操亚洲美女惩罚| 亚洲影院色无极综合| 欧美激情一区二区三区四区 | 噜噜噜在线观看免费视频日韩| 亚洲图片激情小说| 欧美日本在线播放| 亚洲国产精品美女|