【问题标题】:How to read XML data from a URL by using Visual C#如何使用 Visual C# 从 URL 读取 XML 数据
【发布时间】:2019-04-16 10:05:23
【问题描述】:

有 1304 个 KeyFamily 组。

例如: XML 格式的季度国民经济核算。

任务是使用 id 和 Name 将所有这 1304 个家庭组合在一个 txt 文件中

这是它在 txt 文件中的样子: QNA|季度国民账户 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文件的结构..你能发布一个链接或显示一个xml示例(带有根和节点)

标签: c# xml


【解决方案1】:

尝试以下使用 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();

        }

    }


}

【讨论】:

  • 非常感谢您的帮助!请点击带有根和节点的xml的链接:stats.oecd.org/restsdmx/sdmx.ashx/GetDataStructure/all
  • xml 无效。包含一个 & 符号:渔业研发支出。见维基:en.wikipedia.org/wiki/…
  • 我更新了代码以使用 XML 链接。如果从 URL 调用如果有效,但如果我将 XML 放入文件中则失败。
  • 能否请您解释一下我如何只能用英文输出:xml:lang="en"?非常感谢!
  • 更改一行:columns.AddRange(keyFamily.Elements().Where(x => (x.Name.LocalName == "Name") && ((string)x.Attributes(). First() == "en")).Select(x => (string)x));
猜你喜欢
  • 2012-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-18
  • 1970-01-01
  • 2021-05-03
相关资源
最近更新 更多