簡體   English   中英

解析 XML 文件的節點

[英]Parse the Nodes of XML files

如何解析給定目錄下的所有 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

這是一個遞歸問題,您正在尋找的稱為“樹遍歷”。 這意味着對於每個子節點,您要查看它的子節點,然后查看該節點的子節點(如果有的話)等等,在進行時記錄“路徑”,但只打印出“葉”節點。

您將需要這樣的函數來“遍歷”樹:

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循環中):

try
{
    // new xdoc instance 
    XmlDocument xDoc = new XmlDocument();

    //load up the xml from the location 
    xDoc.Load(sitemap);

    // start traversing from the children of the root node
    var rootNode = xDoc.FirstChild;
    traverse(rootNode.ChildNodes, rootNode.Name);
}
catch
{
    Console.WriteLine("Error :-(");
}

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

希望這可以幫助! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM