0

Hello can somebody please show me how to make reading this xml in c# work properly, I've tried alot and i can't get it to work, Help is very much appreciated!

I create this xml in php:

  <?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>

I'm trying to parse it in c# like this:

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

                            reader.ReadEndElement();
                        }

Can anybody see what I'm doing wrong here?

I get no title in my results.

whats wrong?

2 Answers 2

1

Since your xml is flat, how about using Linq and loading it to a dictionary

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: To read directly from file you can use XDocument.Load(filename)

Sign up to request clarification or add additional context in comments.

2 Comments

i tried this but got web exception var response = new WebClient().DownloadString("citfree.com/cronjobs/imdb/fetch.php?url=" + IMDB); var doc = XDocument.Parse(response).Root.Elements(); var dict = doc.ToDictionary(e => e.Name.LocalName, e => (string)e); Console.WriteLine(dict["budget"]);
@Dean See the updated answer. I added a Trim, since your site prepends a blank line into the xml.
1

So you want to have an object in C# that holds the XML representation?

Try this:

XDocument doc = XDocument.Load("data.xml");

2 Comments

im getting exception using this too.
Can you update your question and provide your current source code?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.