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

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

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

導航

統計

  • 隨筆 - 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>
              在线观看国产成人av片| 噜噜噜在线观看免费视频日韩 | 欧美一区成人| 亚洲少妇诱惑| 国产欧美日韩综合一区在线播放| 久久成人亚洲| 久久亚洲电影| 亚洲午夜精品视频| 99热这里只有精品8| 国产精品乱码妇女bbbb| 久久蜜桃av一区精品变态类天堂| 久久精品国产视频| 亚洲精品一品区二品区三品区| 日韩性生活视频| 国产日韩欧美成人| 亚洲激情电影在线| 欧美性猛片xxxx免费看久爱| 久久国产手机看片| 亚洲欧美卡通另类91av| 狠狠久久五月精品中文字幕| 欧美刺激性大交免费视频| 欧美日韩成人一区二区| 欧美一区二区视频在线观看2020| 狂野欧美性猛交xxxx巴西| 亚洲综合国产| 巨乳诱惑日韩免费av| 亚洲欧美日韩天堂一区二区| 久久久久久一区| 亚洲午夜一二三区视频| 久久免费一区| 亚欧成人在线| 欧美精品成人一区二区在线观看| 久久精品一区四区| 国产精品av免费在线观看| 欧美激情精品久久久久久蜜臀| 国产精品成人一区二区艾草| 欧美福利在线| 国产婷婷色一区二区三区在线| 亚洲欧洲一级| 在线高清一区| 欧美一区二区三区在线观看视频| 在线一区观看| 欧美v国产在线一区二区三区| 欧美在线观看视频一区二区三区 | 六月婷婷久久| 久久裸体视频| 国产欧美日本| 亚洲综合视频1区| 亚洲午夜日本在线观看| 欧美日韩国产不卡在线看| 欧美成黄导航| 国内精品久久久久影院优| 亚洲男人的天堂在线aⅴ视频| 亚洲一级高清| 欧美视频精品在线| 日韩午夜黄色| 一区二区三区视频观看| 欧美精品一区二区三区很污很色的 | 在线亚洲+欧美+日本专区| 欧美高清视频在线观看| 欧美激情国产日韩| 亚洲人成在线播放网站岛国| 欧美凹凸一区二区三区视频| 欧美大片一区二区三区| 亚洲日韩视频| 欧美激情一区二区三区在线 | 欧美亚洲综合久久| 国产欧美一区二区三区在线老狼| 亚洲午夜电影在线观看| 性欧美xxxx大乳国产app| 国产精品久久久久久久久免费樱桃 | 一个色综合av| 性欧美videos另类喷潮| 国产在线不卡| 美女日韩在线中文字幕| 91久久久精品| 亚洲综合二区| 国产亚洲欧洲997久久综合| 欧美在线视频播放| 欧美成年人视频| 一区二区高清视频| 国产精品日韩专区| 久久精品成人一区二区三区蜜臀 | 亚洲免费视频网站| 国产日韩欧美一区二区三区四区| 欧美一区二区三区日韩| 欧美激情视频一区二区三区在线播放 | 欧美精品一区二区三区在线播放| 亚洲毛片av在线| 欧美一级午夜免费电影| 精久久久久久| 欧美日韩成人一区二区| 欧美亚洲一区二区在线| 亚洲激情黄色| 午夜精品久久久久影视| 在线精品一区| 国产精品久久久久久妇女6080| 久久精品国产成人| 亚洲毛片网站| 久久这里有精品15一区二区三区 | 国内精品美女在线观看| 欧美日本亚洲| 久久精品女人| 99精品欧美一区| 免费高清在线视频一区·| 中文有码久久| 亚洲国产精品高清久久久| 国产精一区二区三区| 欧美激情亚洲一区| 久久亚洲精品视频| 亚洲欧美日韩在线不卡| 日韩一区二区精品| 欧美国产欧美亚洲国产日韩mv天天看完整 | 另类激情亚洲| 午夜精品电影| 一区二区三区 在线观看视频| 美国成人直播| 欧美在线观看视频一区二区三区 | 欧美在线免费视屏| 亚洲一区二区三区免费观看 | 国产日韩欧美精品综合| 国产精品第2页| 欧美激情精品久久久久| 久久久国产午夜精品| 亚洲在线电影| 亚洲一区二区免费在线| 亚洲精品欧美日韩专区| 欧美肥婆在线| 久久美女艺术照精彩视频福利播放| 亚洲综合不卡| 亚洲综合色自拍一区| 亚洲深夜福利网站| 一区二区三区久久精品| 亚洲精品视频中文字幕| 亚洲精品123区| 亚洲国产精品久久久久秋霞不卡| 国产自产在线视频一区| 国产一区二区三区无遮挡| 国产欧美精品xxxx另类| 国产精品乱码| 国产欧美一区二区三区久久人妖 | 国产一区二区三区四区三区四| 国产精品免费视频观看| 国产精品vvv| 国产精品久久福利| 国产精品亚洲激情| 国产区欧美区日韩区| 国产欧美精品在线观看| 国产在线视频欧美一区二区三区| 国产欧美日韩精品丝袜高跟鞋| 国产精品久久久久77777| 国产精品久久久久婷婷| 国产精品主播| 狠狠v欧美v日韩v亚洲ⅴ| 亚洲高清色综合| 亚洲精品国产系列| 亚洲视频在线免费观看| 午夜精品久久久久久久| 久久成人精品电影| 欧美1级日本1级| 亚洲精品美女91| 亚洲欧美久久久久一区二区三区| 欧美一区二视频| 美女黄网久久| 欧美日韩一区二区在线播放| 国产精品色一区二区三区| 精品91在线| aa日韩免费精品视频一| 亚洲欧美日本国产有色| 久久精品国产欧美亚洲人人爽| 久久综合婷婷| 99在线精品视频| 久久久精品日韩欧美| 欧美激情视频网站| 国产麻豆精品久久一二三| 亚洲福利在线看| 亚洲综合国产激情另类一区| 蜜臀a∨国产成人精品| 一区二区动漫| 美女精品视频一区| 国产精品久久久久一区| 亚洲黄色一区| 欧美一区观看| 亚洲黄色三级| 欧美一区三区三区高中清蜜桃| 你懂的亚洲视频| 国产欧美激情| 亚洲天堂av在线免费观看| 久久久999精品| 一区二区日韩伦理片| 久久在精品线影院精品国产| 国产精品网曝门| 夜色激情一区二区| 免费久久99精品国产自| 午夜精品久久久99热福利| 国产精品v一区二区三区 | 国产美女精品免费电影| 一区二区欧美在线观看| 亚洲成人直播| 久久久久久久国产|