利用c#语言解析xml文件,如何在C#中读取和解析XML文件?

这是我为阅读xml站点 Map 而编写的应用程序:

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.IO;

using System.Data;

using System.Xml;

namespace SiteMapReader

{

class Program

{

static void Main(string[] args)

{

Console.WriteLine("Please Enter the Location of the file");

// get the location we want to get the sitemaps from

string dirLoc = Console.ReadLine();

// get all the sitemaps

string[] sitemaps = Directory.GetFiles(dirLoc);

StreamWriter sw = new StreamWriter(Application.StartupPath + @"\locs.txt", true);

// loop through each file

foreach (string sitemap in sitemaps)

{

try

{

// new xdoc instance

XmlDocument xDoc = new XmlDocument();

//load up the xml from the location

xDoc.Load(sitemap);

// cycle through each child noed

foreach (XmlNode node in xDoc.DocumentElement.ChildNodes)

{

// first node is the url ... have to go to nexted loc node

foreach (XmlNode locNode in node)

{

// thereare a couple child nodes here so only take data from node named loc

if (locNode.Name == "loc")

{

// get the content of the loc node

string loc = locNode.InnerText;

// write it to the console so you can see its working

Console.WriteLine(loc + Environment.NewLine);

// write it to the file

sw.Write(loc + Environment.NewLine);

}

}

}

}

catch { }

}

Console.WriteLine("All Done :-)");

Console.ReadLine();

}

static void readSitemap()

{

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值