Csharp/CSharp Tutorial/XML/Xml Read
Содержание
Load xml from xml file directly
<source lang="csharp">using System; using System.Xml; class MainClass {
static void Main(string[] args) { string filename = @"C:\books.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(filename);
xmlDoc.Save(Console.Out);
}
}</source>
Read XML content as double
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml;
public class MainClass { public static void Main() { XmlReader reader; double totalPrice=0; reader = XmlReader.Create("pubs.xml"); while (reader.Read()) { if (reader.IsStartElement() && reader.Name=="titles") { reader.MoveToAttribute("price"); totalPrice += reader.ReadContentAsDouble(); } } } }</source>
Read XML from URL
<source lang="csharp">using System; using System.IO; using System.Xml; class MainClass {
static void Main(string[] args) { string localURL = "http:\\www.yoursite.ru\\Test.xml"; XmlTextReader myXmlURLreader = null; myXmlURLreader = new XmlTextReader (localURL); while (myXmlURLreader.Read()) { //TODO - } if (myXmlURLreader != null) myXmlURLreader.Close(); }
}</source>
Set Book As Current
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; public class MainClass {
public static void Main() { XmlDocument mDocument = new XmlDocument(); XmlNode mCurrentNode; mDocument.Load("XPathQuery.xml"); mCurrentNode = mDocument.DocumentElement; mCurrentNode = mCurrentNode.SelectSingleNode("book[title="C#"]"); RecurseXmlDocumentNoSiblings(mCurrentNode); } static void DisplayList(XmlNodeList nodeList) { foreach (XmlNode node in nodeList) { RecurseXmlDocumentNoSiblings(node); } } static void RecurseXmlDocumentNoSiblings(XmlNode root) { if (root is XmlElement) { Console.WriteLine(root.Name); if (root.HasChildNodes) RecurseXmlDocument(root.FirstChild); } else if (root is XmlText) { string text = ((XmlText)root).Value; Console.WriteLine(text); } else if (root is XmlComment) { string text = root.Value; Console.WriteLine(text); if (root.HasChildNodes) RecurseXmlDocument(root.FirstChild); } } static void RecurseXmlDocument(XmlNode root) { if (root is XmlElement) { Console.WriteLine(root.Name); if (root.HasChildNodes) RecurseXmlDocument(root.FirstChild); if (root.NextSibling != null) RecurseXmlDocument(root.NextSibling); } else if (root is XmlText) { string text = ((XmlText)root).Value; Console.WriteLine(text); } else if (root is XmlComment) { string text = root.Value; Console.WriteLine(text); if (root.HasChildNodes) RecurseXmlDocument(root.FirstChild); if (root.NextSibling != null) RecurseXmlDocument(root.NextSibling); } }
}</source>
Set up XmlReaderSettings
<source lang="csharp">using System; using System.Collections.Generic; using System.ruponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Schema;
public class MainClass { public static void Main() { XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; settings.ValidationType = ValidationType.DTD; //settings.ValidationType=ValidationType.Schema; //settings.Schemas.Add("", ""); settings.ValidationEventHandler += new ValidationEventHandler(OnValidationError); XmlReader reader=XmlReader.Create("value", settings); while (reader.Read()) { } reader.Close(); } static void OnValidationError(object sender, ValidationEventArgs e) { MessageBox.Show(e.Message); } }</source>
To read from an XML file
<source lang="csharp">using System; using System.IO; using System.Xml; class MainClass {
static void Main(string[] args) { XmlTextReader reader = new XmlTextReader("Sample.xml"); // Read the File while (reader.Read()) { } if (reader != null) reader.Close(); }
}</source>