سلام 😉 در این مطلب آموزش استفاده از XmlTextReader برای لود کردن xml document در c#.net را آماده کردم. انشاالله که این آموزش برای شما مفید باشد.
خروجی کد به این صورت می باشد :
MyTestElements(Element: )
TestBoolean(Element: )
#text(Text: true)
TestBoolean(Element: )
#text(Text: true)
کد استفاده از XmlTextReader برای لود کردن xml document در c#.net :
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);
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);