【问题标题】:Display field from XML Response Data using C# Console使用 C# 控制台显示来自 XML 响应数据的字段
【发布时间】:2020-06-07 10:36:51
【问题描述】:

我有以下 xml 响应数据。现在我需要使用 C# 控制台应用程序显示 ProjectName 和 MigrationStatus。我是 C# 新手,请指导我从 xml 节点列表中获取字段名称。我已经尝试了下面的代码,但混淆了很多。 提前致谢

XML 响应代码:

<?xml version="1.0" encoding="utf-8"?>
<feed xml:base="https://URL"
    <id><<URL>></id>
    <title type="text">Projects</title>
    <updated>2020-06-07T06:24:57Z</updated>
    <link rel="self" title="Projects" href="Projects" />
    <entry>
        <id><<URL>>(guid'86c492af-8c63-ea11-b119-00155d6c5103')</id>
        <category term="ReportingData.Project" scheme="<<URL>>" />
        <link rel="edit" title="Project" href="Projects(guid'86c492af-8c63-ea11-b119-00155d6c5103')" />
        <title />
        <updated>2020-06-07T06:24:57Z</updated>
        <author>
            <name />
        </author>
        <content type="application/xml">
            <m:properties>
                <d:NOde1>BULLL</d:ProjectName>
                <d:NOde2>DOM</d:NOde2>
            </m:properties>
        </content>
    </entry>
 </feed>

【问题讨论】:

  • 你提到了代码,你还在编辑问题吗?
  • 这能回答你的问题吗? printing list of xml nodes in c#
  • 到目前为止,您是否尝试过 C# 中的任何东西?

标签: c# xml


【解决方案1】:

尝试使用 xml linq 解析文件

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement feed = doc.Root;
            XNamespace ns = feed.GetDefaultNamespace();
            XNamespace nsM = feed.GetNamespaceOfPrefix("m");
            XNamespace nsD = feed.GetNamespaceOfPrefix("d");

            List<Entry> entries = doc.Descendants(ns + "entry").Select(x => new Entry()
            {
                id = (string)x.Element(ns + "id"),
                title = (string)x.Element(ns + "title"),
                updated = (DateTime)x.Element(ns + "updated"),
                author_name = (string)x.Descendants(ns + "name").FirstOrDefault(),
                ProjectName = (string)x.Descendants(nsD + "ProjectName").FirstOrDefault(),
                MigrationStatus = (string)x.Descendants(nsD + "MigrationStatus").FirstOrDefault()
            }).ToList();

            Dictionary<string, Entry> dict = entries
                .GroupBy(x => x.ProjectName, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

            foreach (KeyValuePair<string, Entry> entry in dict)
            {
                Console.WriteLine(entry.Key);
            }
        }
    }
    public class Entry
    {
         public string id { get; set; }
         public string title { get; set; }
         public DateTime updated { get; set; }
         public string author_name { get; set; }
         public string ProjectName { get; set; }
         public string MigrationStatus { get; set; }
    }
}

【讨论】:

  • 我在一个变量中有上面的 xml 响应,我从 ODATA URL 得到它。你能请教怎么做吗..
  • 您可以将 FILENAME 替换为 URL,以便直接下载。我将添加一个字典,以便您可以按项目名称查找。
  • 非常感谢 jdweng。请让我知道如何在控制台上写它。?提前致谢。这是我的最后一个问题..
  • 你想要怎样的输出?使用 Console.WriteLine();
  • 尝试过(Console.WriteLine(Entry.ProjectName);) 但无法使用console.writeline(); 显示项目名称;我想从 XML 响应正文中显示所有 ProjectNames..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-07-27
  • 2011-12-15
  • 1970-01-01
  • 2015-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多