Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

There are 1304 KeyFamily groups.

As an example: Quarterly National Accounts in XML.

The task is to combine all these 1304 families in one txt file using id and Name

This is the way how it must look like in txt file: QNA|Quartily National Accounts PAT_IND|..... ....|....

 while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element:
                    Console.Write(reader.Value);

                    while (reader.MoveToNextAttribute()) 
                        Console.Write(reader.Value + "'");
                    Console.Write(">");
                    Console.WriteLine(">");
                    break;
                case XmlNodeType.Text: 
                    Console.WriteLine(reader.Value);
                    break;
                case XmlNodeType.EndElement: 
                    Console.Write("</" + reader.Name);
                    Console.WriteLine(">");
                    break;
            }
        }

<KeyFamily id="QNA" agencyID="OECD">...</KeyFamily>
<KeyFamily id="PAT_IND" agencyID="OECD">...</KeyFamily>
<KeyFamily id="SNA_TABLE11" agencyID="OECD">...</KeyFamily>
<KeyFamily id="EO78_MAIN" agencyID="OECD">
<Name xml:lang="en">
Economic Outlook No 78 - December 2005 - Annual Projections for OECD Countries
</Name>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
181 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try following which uses xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;


namespace ConsoleApplication108
{
    class Program
    {
        const string XML_FILENAME = @"c:	emp	est.xml";
        const string URL = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/all";
        const string TEXT_FILENAME = @"c:	emp	est.txt";
        static void Main(string[] args)
        {
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.CheckCharacters = false;
            //XmlReader reader = XmlReader.Create(XML_FILENAME, settings);
            XmlReader reader = XmlReader.Create(URL, settings);
            StreamWriter writer = new StreamWriter(TEXT_FILENAME);
            while (!reader.EOF)
            {
                if (reader.Name != "KeyFamily")
                {
                    reader.ReadToFollowing("KeyFamily");
                }
                if (!reader.EOF)
                {
                    XElement keyFamily = (XElement)XElement.ReadFrom(reader);

                    List<string> columns = new List<string>();
                    columns.Add((string)keyFamily.Attribute("id"));
                    //string agencyID = (string)keyFamily.Attribute("agencyID");
                    columns.AddRange(keyFamily.Elements().Where(x => x.Name.LocalName == "Name").Select(x => (string)x));

                    writer.WriteLine(string.Join("|", columns));
                }
            }
            writer.Flush();
            writer.Close();

        }

    }


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...