简体   繁体   中英

LINQ to Query XML in C#

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, eg, no where , and it seems simple, but I'm having terminal brain fade trying to get this done.

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
});

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

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"));
    });
}

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
                });
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM