Csharp/CSharp Tutorial/XML/XmlTextReader
Содержание
- 1 Copy one xml document as a sub element into another xml document
- 2 Create XmlTextReader from StringReader
- 3 Load xml document in XmlTextReader to XmlDocument
- 4 Read and write XML document
- 5 Use XmlTextReader to load xml document from file
- 6 XmlTextReader: read and move
- 7 XmlTextReader: Skip
- 8 XmlTextWriter: Write Comment
- 9 XmlTextWriter: write start element
- 10 XmlTextWriter: write string
Copy one xml document as a sub element into another xml document
<source lang="csharp">using System; using System.Xml; class MainClass {
static void Main(string[] args) { XmlTextWriter writer = new XmlTextWriter("C:\\xmlWriterTest.xml", null); writer.WriteStartDocument();
XmlTextReader reader = new XmlTextReader(@"C:\Sample.xml"); while (!reader.EOF) { writer.WriteNode(reader, false); }
// Ends the document. writer.WriteEndDocument(); writer.Close(); return; }
}</source>
Create XmlTextReader from StringReader
<source lang="csharp">using System; using System.IO; using System.Xml; class MainClass {
static void Main(string[] args) { //StringReader stream = new StringReader("Sample.xml"); // Thanks for Scott R (rradjah at yahoo.ru) StringReader stream = new StringReader("<?xml version="1.0"?><foo></foo>"); XmlTextReader reader = new XmlTextReader(stream); while (reader.Read()) { //TODO - } if (reader != null) reader.Close(); }
}</source>
Load xml document in XmlTextReader to XmlDocument
<source lang="csharp">using System; using System.Xml; class MainClass {
public static void Main() { XmlTextReader xtr = new XmlTextReader(@"c:\test.xml"); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument xd = new XmlDocument(); xd.Load(xtr); XmlNode xnodDE = xd.DocumentElement; ChildDisplay(xnodDE, 0); xtr.Close(); } private static void ChildDisplay(XmlNode xnod, int level) { XmlNode xnodWorking; String pad = new String(" ", level * 2); Console.WriteLine(pad + xnod.Name + "(" + xnod.NodeType.ToString() + ": " + xnod.Value + ")"); if (xnod.NodeType == XmlNodeType.Element) { XmlNamedNodeMap mapAttributes = xnod.Attributes; for(int i=0; i<mapAttributes.Count; i++) { Console.WriteLine(pad + " " + mapAttributes.Item(i).Name + " = " + mapAttributes.Item(i).Value); } } if (xnod.HasChildNodes) { xnodWorking = xnod.FirstChild; while (xnodWorking != null) { ChildDisplay(xnodWorking, level+1); xnodWorking = xnodWorking.NextSibling; } } }
}</source>
MyTestElements(Element: ) TestBoolean(Element: ) #text(Text: true)
Read and write XML document
<source lang="csharp">using System; using System.Collections; using System.Data; using System.Xml;
class MainClass{
public static void Main(){ XmlDocument doc = new XmlDocument(); // read doc.Load( "Sample.xml" ); Console.WriteLine(doc.OuterXml); // write XmlTextWriter tw = new XmlTextWriter( "testOut.xml", null ); tw.Formatting = Formatting.Indented; tw.Indentation = 4; doc.Save( tw ); tw.Close(); }
}</source>
<?xml version="1.0" encoding="utf-8"?><scientists xmlns="http://tempuri.org/sample.xsd"><physicist>< name>Newton</name><discovery>gravity</discovery></physicist><chemist><name>Curie</name><discovery>ra dioactivity</discovery></chemist><herpetologist><name>Steve Irwin</name><discovery>they bite!</disco very></herpetologist></scientists>
Use XmlTextReader to load xml document from file
<source lang="csharp">using System; using System.Xml; class MainClass {
public static void Main() { XmlTextReader xtr = new XmlTextReader(@"c:\test.xml"); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument xd = new XmlDocument(); xd.Load(xtr); XmlNode xnodDE = xd.DocumentElement; ChildDisplay(xnodDE, 0); xtr.Close(); } private static void ChildDisplay(XmlNode xnod, int level) { XmlNode xnodWorking; String pad = new String(" ", level * 2); Console.WriteLine(pad + xnod.Name + "(" + xnod.NodeType.ToString() + ": " + xnod.Value + ")"); if (xnod.NodeType == XmlNodeType.Element) { XmlNamedNodeMap mapAttributes = xnod.Attributes; for(int i=0; i<mapAttributes.Count; i++) { Console.WriteLine(pad + " " + mapAttributes.Item(i).Name + " = " + mapAttributes.Item(i).Value); } } if (xnod.HasChildNodes) { xnodWorking = xnod.FirstChild; while (xnodWorking != null) { ChildDisplay(xnodWorking, level+1); xnodWorking = xnodWorking.NextSibling; } } }
}</source>
MyTestElements(Element: ) TestBoolean(Element: ) #text(Text: true)
XmlTextReader: read and move
<source lang="csharp">using System; using System.Xml; class MainClass {
static void Main(string[] args) { XmlTextReader reader = new XmlTextReader(@"C:\books.xml"); if (reader.Read()) { Console.WriteLine(reader.Name); reader.MoveToContent(); Console.WriteLine(reader.Name); } }
}</source>
XmlTextReader: Skip
<source lang="csharp">using System; using System.Drawing; using System.Collections; using System.ruponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; using System.Xml; public class MainClass{
public static void Main(){ XmlTextReader reader = new XmlTextReader(@"C:\books.xml"); while (reader.Read()) { if (reader.Name != "bookstore") reader.Skip(); else { Console.WriteLine("Name: "+reader.Name); Console.WriteLine("Level of the node: " +reader.Depth.ToString()); Console.WriteLine("Value: "+reader.Value); } } }
}</source>
XmlTextWriter: Write Comment
<source lang="csharp">using System; using System.Xml; class MainClass {
static void Main(string[] args) { XmlTextWriter writer = new XmlTextWriter("C:\\xmlWriterTest.xml", null); writer.WriteStartDocument(); // Write comments writer.WriteComment("This program uses XmlTextWriter."); // Ends the document. writer.WriteEndDocument(); writer.Close(); return; }
}</source>
XmlTextWriter: write start element
<source lang="csharp">using System; using System.Xml; class MainClass {
static void Main(string[] args) { XmlTextWriter writer = new XmlTextWriter("C:\\xmlWriterTest.xml", null); writer.WriteStartDocument(); // Write first element writer.WriteStartElement("root"); writer.WriteStartElement("r", "RECORD", "urn:record"); // Ends the document. writer.WriteEndDocument(); writer.Close(); return; }
}</source>
XmlTextWriter: write string
<source lang="csharp">using System; using System.Xml; class MainClass {
static void Main(string[] args) { XmlTextWriter writer = new XmlTextWriter("C:\\xmlWriterTest.xml", null); writer.WriteStartDocument(); // Write next element writer.WriteStartElement("FirstName",""); writer.WriteString("Mahesh"); writer.WriteEndElement(); // Ends the document. writer.WriteEndDocument(); writer.Close(); return; }
}</source>