Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Load xml from xml file directly
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);
}
}
Read XML content as double
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();
}
}
}
}
Read XML from URL
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();
}
}
Set Book As Current
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);
}
}
}
Set up XmlReaderSettings
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);
}
}
To read from an XML file
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();
}
}