如何在c#中读取和解析XML文件?


当前回答

public void ReadXmlFile()
{
    string path = HttpContext.Current.Server.MapPath("~/App_Data"); // Finds the location of App_Data on server.
    XmlTextReader reader = new XmlTextReader(System.IO.Path.Combine(path, "XMLFile7.xml")); //Combines the location of App_Data and the file name
    while (reader.Read())
    {
        switch (reader.NodeType)
        {
            case XmlNodeType.Element:
                break;
            case XmlNodeType.Text:
                columnNames.Add(reader.Value);
                break;
            case XmlNodeType.EndElement:
                break;
        }
    }
}

可以避免使用第一个语句,只在XmlTextReader的构造函数中指定路径名。

其他回答

下面是我写的一个读取xml站点地图的应用程序:

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()
        {
        }
    }
}

粘贴箱代码 http://pastebin.com/yK7cSNeY

例如,检查XmlTextReader类。

Linq到XML。

同时,VB。NET通过编译器提供了比c#更好的xml解析支持。如果你有这个选择和愿望,那就去看看吧。

你可以:

使用XmlSerializer类 使用XmlDocument类

示例在msdn页面上提供

下面是另一种使用Cinchoo ETL的方法,它是一个开源库,可以用很少的代码行解析xml文件。

using (var r = ChoXmlReader<Item>.LoadText(xml)
       .WithXPath("//item")
      )
{
    foreach (var rec in r)
        rec.Print();
}

public class Item
{
    public string Name { get; set; }
    public string ProtectionLevel { get; set; }
    public string Description { get; set; }
}

样本提琴:https://dotnetfiddle.net/otYq5j

免责声明:我是这个库的作者。