C# 如何在C控制台程序中从URL加载XML并将内容放入变量中

C# 如何在C控制台程序中从URL加载XML并将内容放入变量中,c#,.net,xml,url,console,C#,.net,Xml,Url,Console,---更新--- 我有一个问题,对于更多的c语言专家来说很简单,我是一个Java异议者,我将感谢您的帮助,在这里使用正确的技术和技术做了一件好事 我需要什么 从URL加载XML文件。 在此之后,我需要在信息集之间导航,获取数据并将其放入一些变量中 这将是一个.NETC可执行程序,独立于COBOL中的其他程序运行并恢复数据 示例XML是: <?xml version="1.0" encoding="utf-8"?> <movie> <title> F

---更新---

我有一个问题,对于更多的c语言专家来说很简单,我是一个Java异议者,我将感谢您的帮助,在这里使用正确的技术和技术做了一件好事

我需要什么

从URL加载XML文件。 在此之后,我需要在信息集之间导航,获取数据并将其放入一些变量中

这将是一个.NETC可执行程序,独立于COBOL中的其他程序运行并恢复数据

示例XML是:

<?xml version="1.0" encoding="utf-8"?>
<movie>
  <title>
    Fast Five
  </title>
  <year>
    2011
  </year>
  <description>
    Dominic Toretto and his crew of street racers plan a massive heist ...
  </description>
  <director>
    <director_1>Justin Lin</director_1>
    <director_2>Vovó Mafalda</director_2>
  </director>
</movie>
但这对我来说很难闻

有没有更有效、更漂亮的方法? 以及如何在控制台C程序中从URL加载XML

感谢所有帮助和关注。

可以从URI中读取:

XmlDocument doc = new XmlDocument();
doc.Load("http://localhost:8080/movie.xml");
var movie = doc.SelectSingleNode("/movies/movie");

从C中读取XML有几种不同的方法。对于简单、小的结构,尤其是远程结构,我发现XmlDocument是最短的方法。

如果您使用的是.NET Framework 3.5或更高版本,我认为您可以使用XDocument类,它具有加载和解析方法。拥有XDocument类后,可以使用Linq to XML构建匿名类型来保存数据,例如:-

const string xmlData = @"<?xml version=""1.0"" encoding=""utf-8""?><movie><title>Fast Five</title><year>2011</year><description>Dominic Toretto and his crew of street racers plan a massive heist to buy their freedom while in the sights of a powerful Brazilian drug lord and a dangerous federal agent.</description><director>Justin Lin</director></movie>";

var xdoc = XDocument.Parse(xmlData);
var movie = from docRoot in xdoc.Descendants("movie")
            let titleElement = docRoot.Element("title") where titleElement != null
            let yearElement = docRoot.Element("year") where yearElement != null
            let descriptionElement = docRoot.Element("description") where descriptionElement != null
            select new
            {
                Title = titleElement.Value,
                Year = yearElement.Value,
                Description = descriptionElement.Value
            };

您可以使用LINQtoXML从XML结构中获取数据。这是关于这个的。基本上,如果这是您的XML:

<movies>
  <movie>
    <title>
      Fast Five
    </title>
    <year>
      2011
    </year>
    <description>
      Dominic Toretto and his crew of street racers plan a massive heist to buy their freedom while in the sights of a powerful Brazilian drug lord and a dangerous federal agent.
    </description>
    <director>
      Justin Lin
    </director>
  </movie>
<movies>
您的代码如下所示:

XElement xelement = XElement.Load("PATH_TO_XML");
IEnumerable<XElement> movies = xelement.Elements();

foreach (var movie in movies)
{
    var title = employee.Element("title").Value);
    //and so on...
}

您在控制台应用程序中这一事实并不重要。无论您在何处处理文档,都将以相同的方式进行处理。这是一个相当琐碎的问题,但我只是想确保您理解,因为您是.NET新手。除此之外,看看XDocument对象,并对LINQ to XML.Hi RGraham进行一些研究。我将使用XmlDocument搜索如何读取文档。给点小费?谢谢你的帮助。我会阅读这篇关于XPath的教程。应该有很多:凯恩,谢谢你的提示。但当我尝试时,生成了一个错误:找不到源类型System.Collections.Generic.IEnumerable的查询模式的实现选择“未找到”。您是否缺少对“System.Core.dll”的引用或对“System.Linq”的using指令?但是项目中的引用是ok System.Core和System.Xml.Linq!
XElement xelement = XElement.Load("PATH_TO_XML");
IEnumerable<XElement> movies = xelement.Elements();

foreach (var movie in movies)
{
    var title = employee.Element("title").Value);
    //and so on...
}