解析XML文件的节点

时间:2017-10-12 09:27:59

标签: c# xml-parsing

如何解析给定目录下的所有XML文件作为应用程序的输入并将其输出写入文本文件。

注意:XML并不总是相同的,XML中的节点可以变化并且具有任意数量的子节点。

任何帮助或指导在这方面都会有所帮助:)

XML文件示例

<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>
<CNT>USA</CNT>
<CODE>3456</CODE>
</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
<CD>
<TITLE>Hide your heart</TITLE>
<ARTIST>Bonnie Tyler</ARTIST>
<COUNTRY>UK</COUNTRY>
<COMPANY>CBS Records</COMPANY>
<PRICE>9.90</PRICE>
<YEAR>1988</YEAR>
</CD>
</CATALOG>

C#代码

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;
using System.Xml.Linq;

namespace XMLTagParser
{
    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)
                        {

                                string loc = locNode.Name;

                                // 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("Error :-(");
                }
            }
            Console.WriteLine("All Done :-)");
            Console.ReadLine();
        }
    }
}

首选输出:

CATALOG/CD/TITLE
CATALOG/CD/ARTIST
CATALOG/CD/COUNTRY/CNT
CATALOG/CD/COUNTRY/CODE
CATALOG/CD/COMPANY
CATALOG/CD/PRICE
CATALOG/CD/YEAR

CATALOG/CD/TITLE
CATALOG/CD/ARTIST
CATALOG/CD/COUNTRY
CATALOG/CD/COMPANY
CATALOG/CD/PRICE
CATALOG/CD/YEAR

1 个答案:

答案 0 :(得分:2)

这是一个递归问题,您正在寻找的是“树遍历”。这意味着,对于每个子节点,您想要查看它的子节点,然后进入该节点的子节点(如果有的话),依此类推,记录“路径”,但只打印出名称'叶'节点。

你需要这样的函数来“遍历”树:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ol id="ColorArrayThemeA"></ol>

然后我会将它添加到您的程序中(在static void traverse(XmlNodeList nodes, string parentPath) { foreach (XmlNode node in nodes) { string thisPath = parentPath; if (node.NodeType != XmlNodeType.Text) { //Prevent adding "#text" at the end of every chain thisPath += "/" + node.Name; } if (!node.HasChildNodes) { //Only print out this path if it is at the end of a chain Console.WriteLine(thisPath); } //Look into the child nodes using this function recursively traverse(node.ChildNodes, thisPath); } } 循环中):

foreach sitemap

我使用了另一个有用的答案:Traverse a XML using Recursive function

希望这有帮助! :)