using System;
using System.IO;
using System.Xml;
class TestXMLReader
{
static void Main(string[] args)
{
TestXMLReader tstObj = new TestXMLReader();
StreamReader myStream = new StreamReader("books.xml");
XmlTextReader xmlTxtRdr = new XmlTextReader(myStream);
while(xmlTxtRdr.Read())
{
if(xmlTxtRdr.NodeType == XmlNodeType.Element
&& xmlTxtRdr.Name == "A")
{
tstObj.ProcessMyDocument(xmlTxtRdr);
}
}
}
public void ProcessMyDocument(XmlTextReader reader)
{
Console.WriteLine("Start processing:" + reader.GetAttribute("property"));
while(!(reader.NodeType == XmlNodeType.EndElement && reader.Name == "B")
&& reader.Read()) {
if(reader.NodeType == XmlNodeType.Element && reader.Name == "C") {
Console.WriteLine("itemcode:" + reader.GetAttribute("c"));
}
}
}
}
/*
<books>
<A property="a">
<B>text</B>
<C c="aaa" >textg</C>
<D>99999</D>
</A>
</books>
*/
XML-RPC
XmlNodeType Text
using System;
using System.Xml;
using System.IO;
using System.Text;
class MainClass {
private static void Main() {
FileStream fs = new FileStream(“products.xml”, FileMode.Create);
XmlWriter w = XmlWriter.Create(fs);
w.WriteStartDocument();
w.WriteStartElement(“products”);
w.WriteStartElement(“product”);
w.WriteAttributeString(“id”, “1001”);
w.WriteElementString(“productName”, “Gourmet Coffee”);
w.WriteElementString(“productPrice”, “0.99”);
w.WriteEndElement();
w.WriteStartElement(“product”);
w.WriteAttributeString(“id”, “1002”);
w.WriteElementString(“productName”, “Blue China Tea Pot”);
w.WriteElementString(“productPrice”, “102.99”);
w.WriteEndElement();
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
fs = new FileStream(“products.xml”, FileMode.Open);
XmlReader r = XmlReader.Create(fs);
while (r.Read()) {
if (r.NodeType == XmlNodeType.Element) {
Console.WriteLine(“<" + r.Name + ">“);
if (r.HasAttributes) {
for (int i = 0; i < r.AttributeCount; i++) {
Console.WriteLine(" ATTRIBUTE: " +
r.GetAttribute(i));
}
}
} else if (r.NodeType == XmlNodeType.Text) {
Console.WriteLine(" VALUE: " + r.Value);
}
}
}
}
[/csharp]
Get XML Nodes in a Specific XML Namespace
using System;
using System.Xml;
public class SelectNodesByNamespace {
private static void Main() {
XmlDocument doc = new XmlDocument();
doc.Load("Order.xml");
XmlNodeList matches = doc.GetElementsByTagName("*","http://mycompany/OrderML");
foreach (XmlNode node in matches) {
Console.Write(node.Name + " ");
foreach (XmlAttribute attribute in node.Attributes) {
Console.Write(attribute.Value + " ");
}
}
}
}
Set Xml Node Value
using System;
using System.Text;
using System.Xml;
//GNU General Public License version 2 (GPLv2)
//http://cbasetest.codeplex.com/license
public class MainClass{
public static void SetXmlNodeValue(XmlNode xmlae, object value)
{
SetXmlNodeValue(xmlae, value, string.Empty);
}
public static void SetXmlNodeValue(XmlNode xmlae, string value)
{
XmlDocument ownerDocument = xmlae.OwnerDocument;
foreach (XmlNode node in xmlae.ChildNodes)
{
if (node.NodeType == XmlNodeType.Text)
{
node.Value = value;
}
}
}
public static void SetXmlNodeValue(XmlNode xmlae, object value, string defaultStr)
{
string str = defaultStr;
if (value != null)
{
str = value.ToString();
}
if (str != null)
{
SetXmlNodeValue(xmlae, str.ToString());
}
}
}
Get attribute from XmlNode
//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion
namespace DbmlManager.Lib.Utility
{
#region Class Docs
/// <summary>
/// Summary description for XmlUtil.
/// </summary>
#endregion
public class XmlUtil
{
#region GetAttrib(XmlNode node, string name)
public static string GetAttrib(XmlNode node, string name)
{
if (node.Attributes == null)
return null;
if (node.Attributes[name] == null)
return null;
return node.Attributes[name].InnerText;
}
#endregion
}
}
Get integer value from xml element
//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion
namespace DbmlManager.Lib.Utility
{
#region Class Docs
/// <summary>
/// Summary description for XmlUtil.
/// </summary>
#endregion
public class XmlUtil
{
#region LoadInt(ref int val, XmlNode parent, string nodeName)
public static void LoadInt(ref int val, XmlNode parent, string nodeName)
{
try
{
XmlElement elem = parent[nodeName];
if (elem != null)
val = Int32.Parse(elem.InnerText.Trim());
}
catch
{
val = 0;
}
}
#endregion
}
}
Load boolean value from xml element
//Microsoft Public License (Ms-PL)
//http://dbmlmanager.codeplex.com/license
#region using
using System;
using System.Xml;
#endregion
namespace DbmlManager.Lib.Utility
{
#region Class Docs
/// <summary>
/// Summary description for XmlUtil.
/// </summary>
#endregion
public class XmlUtil
{
#region LoadBool(ref bool val, XmlNode parent, string nodeName)
public static void LoadBool(ref bool val, XmlNode parent, string nodeName)
{
try
{
XmlElement elem = parent[nodeName];
if (elem != null)
val = (elem.InnerText.Trim() == "1");
}
catch
{
val = false;
}
}
#endregion
}
}