Deleting a Specific Node with Remove

image_pdfimage_print
   
 
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Linq;
public class MainClass {
    public static void Main() {
        XElement firstParticipant;
        XDocument xDocument = new XDocument(
          new XElement("Books", firstParticipant =
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "A"),
              new XElement("LastName", "B")),
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "C"),
              new XElement("LastName", "D"))));
        Console.WriteLine(xDocument);
        firstParticipant.Remove();
        Console.WriteLine(xDocument);
    }
}

    


use Linq to query an XML document

image_pdfimage_print
   
 
using System;
using System.Linq;
using System.Xml;
using System.Xml.Linq;


static class HelloLinqToXml {
    static void Main() {
        var books = new[] {
      new {Title="A", Publisher="M", Year=2005 },
      new {Title="W", Publisher="M", Year=2006 },
      new {Title="R", Publisher="M", Year=2006 }
    };
        XElement xml = new XElement("books",
          from book in books
          where book.Year == 2006
          select new XElement("book",
            new XAttribute("title", book.Title),
            new XElement("publisher", book.Publisher)
          )
        );
        Console.WriteLine(xml);
    }
}

    


Query string array by its element length

image_pdfimage_print

using System;
using System.Linq;

class HelloWorld {
static void Main() {
string[] words = { “hello”, “wonderful”, “linq”, “beautiful”, “world” };
var shortWords = from word in words
where word.Length <= 5 select word; foreach (var word in shortWords) Console.WriteLine(word); } } [/csharp]

Use Linq query to get XML document elements

image_pdfimage_print
   
 

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

class Program {
    static void Main(string[] args) {
        XElement xml = XElement.Load("Employee.xml");
        XElement html = new XElement("HTML",
                                new XElement("BODY",
                                    new XElement("TABLE",
                                        new XElement("TH", "ID"),
                                        new XElement("TH", "Full Name"),
                                        new XElement("TH", "Role"),
                                            from p in xml.Descendants("person")
                                            join r in xml.Descendants("role") on (int)p.Element("idrole") equals (int)r.Element("id")
                                            select new XElement("TR",
                                                            new XElement("TD", p.Element("id").Value),
                                                            new XElement("TD", p.Element("firstname").Value + " " + p.Element("lastname").Value),
                                                            new XElement("TD", r.Element("roledescription").Value)))));

        html.Save(@"C:People.html");
    }
}

    


Query XML with namespace

image_pdfimage_print
   
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;

class Program {
    static void Main(string[] args) {
        XElement xml = XElement.Load("Hello.xml");
        XNamespace o = "urn:schemas-microsoft-com:office:office";
        var query = from w in xml.Descendants(o + "Author")
                    select w;
        foreach (var record in query)
            Console.WriteLine("Author: {0}", (string)record);

    }
}

    


Get Node by type with OfType

image_pdfimage_print
   
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;

class Program {
    static void Main(string[] args) {
        XElement xml = XElement.Load("Employee.xml");
        IEnumerable<XComment> record = xml.Nodes().OfType<XComment>();
        foreach (XComment comment in record)
            Console.WriteLine(comment);
    }
}

    


Query XML document with Ancestors and First

image_pdfimage_print
   
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;

class Program {
    static void Main(string[] args) {
        XElement xml = XElement.Load("Employee.xml");
        var record = xml.Descendants("firstname").First();
        foreach (var tag in record.Ancestors())
            Console.WriteLine(tag.Name);
    }
}