0

I have a very simple XML document

<content>
    <reference>
        <title>www</title>
        <url>http://xxx</url>
    </reference>
    <reference>
        <title>yyy</title>
        <url>http://zzz</url>
   </reference>
</content>

I'm trying to use LINQ to query each reference in turn to extract text in both title and url. There's nothing conditional in any of this, e.g., no where, and it seems simple, but I'm having terminal brain fade trying to get this done.

2
  • what's the format of the result you expect? Commented May 27, 2015 at 12:00
  • try this to load the doc "XDocument doc = XDocument.Load("yourfile.xml");" Commented May 27, 2015 at 12:02

4 Answers 4

4

You should start by instantiating a new XElement

XElement root = XElement.Parse(xmlString);

Then you can get all the child reference elements:

IEnumerable<XElement> references = root.Elements("references");

Let's suppose now that we have a class that holds a title and a url:

public class Info {
     public string Title { get; set; }
     public string Url { get; set; }
}

We can map our reference elements to these classes:

IEnumerable<Info> infos = references.Select(r => new Info() {
        Title = r.Element("title").Value,
        Url = r.Element("url").Value
});
Sign up to request clarification or add additional context in comments.

2 Comments

That's just great; it produces the result and additionally adds a nice touch with the Info object. Very many thanks!
@peedurrr Glad I could be of help! ;)
1
void Main()
{
    var xml = @"<content>
                    <reference>
                        <title>www</title>
                        <url>http://xxx</url>
                    </reference>
                    <reference>
                        <title>yyy</title>
                        <url>http://zzz</url>
                    </reference>
                </content>";


    XElement.Parse(xml).Elements("reference").ToList().ForEach(x => 
    {
        Console.WriteLine("Title: {0}, URL: {1}", x.Element("title"), x.Element("url"));
    });
}

Comments

1

You can use XDocument class with LINQ following way for it:

string MyXml = @"<RootNode>
                 ........
                 ........
                 </RootNode>";

XDocument xdoc= XDocument.Parse(MyXml);

var result =  from reference in xdoc.Descendants("reference")
              select new
                    {
                       Title = reference.Element("title").Value,
                       Url = reference.Element("url").Value
                    };

See this working DEMO

Comments

0

Try this

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

namespace ConsoleApplication30
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
                "<content>" +
                    "<reference>" +
                        "<title>www</title>" +
                        "<url>http://xxx</url>" +
                    "</reference>" +
                    "<reference>" +
                        "<title>yyy</title>" +
                        "<url>http://zzz</url>" +
                   "</reference>" +
                "</content>";

            XDocument doc = XDocument.Parse(input);

            var results = doc.Descendants("reference")
                .Select(x => new {
                    title = x.Descendants("title").FirstOrDefault().Value,
                    url = x.Descendants("url").FirstOrDefault().Value
                });
        }
    }
}

Comments

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.