0

您好,有人可以告诉我如何使在 c# 中读取此 xml 正常工作,我已经尝试了很多,但我无法让它工作,非常感谢帮助!

我在 php 中创建了这个 xml:

  <?xml version="1.0" ?> 
- <root>
  <budget>n/A</budget> 
  <cast>Robert De Niro / Katherine Heigl / Diane Keaton / Amanda Seyfried / Topher Grace / Susan Sarandon / Robin Williams / Ben Barnes / Christine Ebersole / David Rasche / Patricia Rae / Ana Ayora / Kyle Bornheimer / Megan Ketch / Christa Campbell</cast> 
  <country>USA</country> 
  <directors>Justin Zackham</directors> 
  <genres>Comedy</genres> 
  <languages>English / Spanish</languages> 
  <discription>A long-divorced couple fakes being married as their family unites for a wedding.</discription> 
  <plot>A long-divorced couple fakes being married as their family unites for a wedding.</plot> 
  <trailer>http://www.imdb.com/video/imdb/vi2079761945/player</trailer> 
  <poster>posters/1931435.jpg</poster> 
  <rating>5.2</rating> 
  <releasedate>26 April 2013 (USA)</releasedate> 
  <runtime>89 min</runtime> 
  <title>It's never too late to start acting like a family.</title> 
  <tagline>It's never too late to start acting like a family.</tagline> 
  <year>2013</year> 
  <votes>1,466</votes> 
  <url>http://www.imdb.com/title/tt1931435/</url> 
  <sites><a href="http://facebook.com/TheBigWeddingMovie" target="_blank">Official Facebook</a> / <a href="http://thebigweddingmovie.com/" target="_blank">Official site</a></sites> 
  </root>

我正在尝试像这样在 c# 中解析它:

                        using (XmlReader reader = XmlReader.Create("data.xml"))
                        {
                            reader.ReadStartElement("root");
                            while (reader.Name == "title")
                            {
                                XElement el = (XElement)XNode.ReadFrom(reader);
                            }

                            reader.ReadEndElement();
                        }

谁能看到我在这里做错了什么?

我的结果中没有标题。

怎么了?

4

2 回答 2

1

因此,您想在 C# 中拥有一个包含 XML 表示的对象吗?

试试这个:

XDocument doc = XDocument.Load("data.xml");
于 2013-05-09T09:27:26.040 回答
1

由于您的 xml 是平面的,如何使用 Linq 并将其加载到字典中

var response = new WebClient().DownloadString("http://citfree.com/cronjobs/imdb/fetch.php?url=Star+Wars");

var dict = XDocument.Parse(response.Trim()).Root
            .Elements()
            .ToDictionary(e => e.Name.LocalName, e => (string)e);

Console.WriteLine(dict["budget"]);

PS:要直接从文件中读取,您可以使用XDocument.Load(filename)

于 2013-05-09T09:28:38.060 回答