簡體   English   中英

如何使用 Visual C# 從 URL 讀取 XML 數據

[英]How to read XML data from a URL by using Visual C#

有 1304 個 KeyFamily 組。

例如:XML 格式的季度國民賬戶。

任務是使用 id 和 Name 將所有這些 1304 個家庭組合在一個 txt 文件中

這就是它在 txt 文件中的樣子: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>

嘗試以下使用 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:\temp\test.xml";
        const string URL = "https://stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/all";
        const string TEXT_FILENAME = @"c:\temp\test.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();

        }

    }


}

暫無
暫無

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

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