| |
Read XML data from xml file: Node type, name |
|
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>
*/
|
|
|
Related examples in the same category |
|