{"id":64466,"date":"2022-04-11T07:50:12","date_gmt":"2022-04-11T05:50:12","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=64466"},"modified":"2022-04-11T08:02:27","modified_gmt":"2022-04-11T06:02:27","slug":"linq-to-xml","status":"publish","type":"post","link":"https:\/\/code-maze.com\/linq-to-xml\/","title":{"rendered":"LINQ to XML"},"content":{"rendered":"<p>LINQ to XML is an in-memory XML programming interface that provides <a href=\"https:\/\/code-maze.com\/linq-csharp-basic-concepts\/\" target=\"_blank\" rel=\"noopener\">LINQ<\/a> functionality to programmers. Like the Document Object Model (DOM), we can use LINQ to XML to load XML documents into memory. However, this way we can process them more efficiently, using the advanced features of LINQ.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/xml-csharp\/LINQtoXML\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s dive in.<\/p>\n<h2>Using LINQ to XML<\/h2>\n<p>Let&#8217;s start by creating an XML document that we will use for the first group of examples. It contains information about three students and the courses they have taken:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;Students&gt;\r\n &lt;Student ID=\"111\"&gt;\r\n   &lt;FirstName&gt;John&lt;\/FirstName&gt;\r\n   &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n   &lt;DateOfBirth&gt;2000-10-2&lt;\/DateOfBirth&gt;\r\n   &lt;Semester&gt;2&lt;\/Semester&gt;\r\n   &lt;Major&gt;Computer Science&lt;\/Major&gt;\r\n   &lt;Courses&gt;\r\n     &lt;Course ID=\"CS103\"&gt;\r\n       &lt;Grade&gt;7.3&lt;\/Grade&gt;\r\n     &lt;\/Course&gt;\r\n     &lt;Course ID=\"CS202\"&gt;\r\n       &lt;Grade&gt;6.9&lt;\/Grade&gt;\r\n     &lt;\/Course&gt;\r\n   &lt;\/Courses&gt;\r\n &lt;\/Student&gt;\r\n &lt;Student ID=\"222\"&gt;\r\n   &lt;FirstName&gt;Jane&lt;\/FirstName&gt;\r\n   &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n   &lt;DateOfBirth&gt;2001-2-22&lt;\/DateOfBirth&gt;\r\n   &lt;Semester&gt;1&lt;\/Semester&gt;\r\n   &lt;Major&gt;Electrical Engineering&lt;\/Major&gt;\r\n   &lt;Courses&gt;\r\n     &lt;Course ID=\"EE111\"&gt;\r\n       &lt;Grade&gt;5.6&lt;\/Grade&gt;\r\n     &lt;\/Course&gt;\r\n     &lt;Course ID=\"EE303\"&gt;\r\n       &lt;Grade&gt;8.8&lt;\/Grade&gt;\r\n     &lt;\/Course&gt;\r\n   &lt;\/Courses&gt;\r\n &lt;\/Student&gt;\r\n &lt;Student ID=\"333\"&gt;\r\n   &lt;FirstName&gt;Jim&lt;\/FirstName&gt;\r\n   &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n   &lt;DateOfBirth&gt;2000-3-12&lt;\/DateOfBirth&gt;\r\n   &lt;Semester&gt;2&lt;\/Semester&gt;\r\n   &lt;Major&gt;Computer Science&lt;\/Major&gt;\r\n   &lt;Courses&gt;\r\n     &lt;Course ID=\"CS103\"&gt;\r\n       &lt;Grade&gt;7.6&lt;\/Grade&gt;\r\n     &lt;\/Course&gt;\r\n     &lt;Course ID=\"CS202\"&gt;\r\n       &lt;Grade&gt;8.2&lt;\/Grade&gt;\r\n     &lt;\/Course&gt;\r\n   &lt;\/Courses&gt;\r\n &lt;\/Student&gt;\r\n&lt;\/Students&gt;<\/pre>\n<p>LINQ to XML supports two types of syntax forms:<\/p>\n<ul>\n<li>Query syntax<\/li>\n<li>Method syntax<\/li>\n<\/ul>\n<p>Let&#8217;s see a simple query that uses the query syntax form:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var studentsXML = XElement.Load(\"Students.xml\");\r\n\r\nvar students = \r\n    from student in studentsXML.Elements(\"Student\")\r\n    select student.Element(\"FirstName\").Value + \" \" + student.Element(\"LastName\").Value;\r\n\r\nforeach(var student in students)\r\n{\r\n    Console.WriteLine(student);\r\n}\r\n<\/pre>\n<p>First, we load the XML from a file using the <code>XElement.Load()<\/code> method. Then, we select all students and we return the concatenation of their first and last name.<\/p>\n<p>Let&#8217;s see the same query in method syntax form:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var studentsXML = XElement.Load(\"Students.xml\"); \r\n\r\nvar students = studentsXML.Elements(\"Student\") \r\n    .Select(student =&gt; student.Element(\"FirstName\").Value + \" \" + student.Element(\"LastName\").Value); \r\n\r\nforeach(var student in students) \r\n{ \r\n    Console.WriteLine(student); \r\n}<\/pre>\n<p>Going forward, we&#8217;re going to use the method syntax form most of the time. However, we&#8217;re going to show both forms in some interesting cases.<\/p>\n<h2>LINQ to XML Main Classes<\/h2>\n<p>In the introductory examples, we used class <code>XElement<\/code> in order to load the XML document into memory.<\/p>\n<p><code>XElement<\/code> is one of the most important classes in LINQ to XML and represents an XML element. Apart from querying an XML tree, we can also use <code>XElement<\/code> to create a new XML tree or modify an existing one.<\/p>\n<p>Other important classes are <code>XAttribute<\/code> and <code>XDocument<\/code>. <code>XAttribute<\/code> represents an XML attribute, while <code>XDocument<\/code> represents the whole XML document (along with XML declaration, processing instructions, and comments). We may choose to use <code>XDocument<\/code> instead of <code>XElement<\/code>, only if we need this extra functionality otherwise, <code>XElement<\/code> is much simpler to use.<\/p>\n<h2>Create XML Tree<\/h2>\n<p>First, let&#8217;s see how to create a new XML tree using LINQ to XML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">XElement students =\r\n    new XElement(\"Students\",\r\n        new XElement(\"Student\",\r\n            new XAttribute(\"ID\", \"111\"),\r\n            new XElement(\"FirstName\", \"John\"),\r\n            new XElement(\"LastName\", \"Doe\"),\r\n            new XElement(\"DateOfBirth\", \"2000-10-2\"),\r\n            new XElement(\"Semester\", \"2\"),\r\n            new XElement(\"Major\", \"Computer Science\"),\r\n            new XElement(\"Courses\",\r\n                new XElement(\"Course\",\r\n                    new XAttribute(\"ID\", \"CS103\"),\r\n                    new XElement(\"Grade\", \"7.3\")\r\n                ),\r\n                new XElement(\"Course\",\r\n                    new XAttribute(\"ID\", \"CS202\"),\r\n                    new XElement(\"Grade\", \"6.9\")\r\n                )\r\n            )\r\n        )\r\n    );<\/pre>\n<p>Here, we use nested <code>XElement<\/code> (and <code>XAttribute<\/code>) objects in order to create the XML tree. We can see that one or more child elements can be passed as parameters to the constructor of the parent element.<\/p>\n<p>Of course, another way to create an XML tree is to use a LINQ to XML query, as we&#8217;ve already seen in our examples.<\/p>\n<h2>Queries with LINQ to XML<\/h2>\n<p>Now, let&#8217;s learn how to make more complex queries with LINQ to XML. There are various ways to search for specific elements in an XML tree.<\/p>\n<p>We can filter by:<\/p>\n<ul>\n<li>Element value<\/li>\n<li>Attribute value<\/li>\n<li>Elements and\/or attributes in nested elements<\/li>\n<\/ul>\n<h3>Query by Element Value<\/h3>\n<p>We can use the value of an XML element to filter an XML tree:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"2\">var students = StudentsXML.Elements(\"Student\")\r\n    .Where(student =&gt; ((DateTime)student.Element(\"DateOfBirth\")).Year == 2000)\r\n    .Select(student =&gt; (string)student.Element(\"FirstName\") \r\n        + \" \" + (string)student.Element(\"LastName\") \r\n        + \" (\" + ((DateTime)student.Element(\"DateOfBirth\")).ToShortDateString() + \")\");\r\n<\/pre>\n<p>We get the students that were born in the year 2000. The returned Enumerable contains strings with the first name, last name, and birth date of the selected students.<\/p>\n<h3>Query by Attribute Value<\/h3>\n<p>We can also filter an XML tree by using the attribute value of an XML element:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"2\">var student = StudentsXML.Elements(\"Student\")\r\n    .Where(student =&gt; (int)student.Attribute(\"ID\") == 222)\r\n    .FirstOrDefault();\r\n<\/pre>\n<p>The query returns the student object with an ID equal to 222.<\/p>\n<h3>Query in Nested Elements<\/h3>\n<p>In this case, we can filter an XML tree based on nested elements with specific characteristics:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3\">var students = StudentsXML.Elements(\"Student\")\r\n    .Where(student =&gt; student.Elements(\"Courses\").Elements(\"Course\")\r\n        .Any(course =&gt; (string)course.Attribute(\"ID\") == \"CS103\"));\r\n<\/pre>\n<p>Here, we query all students that have taken the course with an ID equal to CS103. In general, we can use any combination of elements and attributes in our queries in order to obtain the desired results.<\/p>\n<h2>Updates with LINQ to XML<\/h2>\n<p>We can also use LINQ to XML in order to modify an XML tree in various ways.<\/p>\n<p>More specifically, we can:<\/p>\n<ul>\n<li>Insert a new element\/attribute<\/li>\n<li>Update an existing element\/attribute<\/li>\n<li>Delete an existing element\/attribute\u00a0<\/li>\n<\/ul>\n<h3>Insert a New Element\/Attribute<\/h3>\n<p>We can modify an existing XML tree by inserting one or more elements or attributes:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"7\">var students = StudentsXML.Elements(\"Student\")\r\n    .Where(student =&gt; (int)student.Element(\"Semester\") == 2\r\n        &amp;&amp; (string)student.Element(\"Major\") == \"Computer Science\");\r\n\r\nforeach (var student in students)\r\n{\r\n    student.Element(\"Courses\").Add(new XElement(\"Course\", new XAttribute(\"ID\", \"CS204\"), new XElement(\"Grade\")));\r\n}\r\n\r\nstudentsXML.Save(\"student_out.xml\");\r\n<\/pre>\n<p>We can see that we have added a new course (CS204) to all students of the 2nd semester. Note that we&#8217;ve chosen to leave the Grade element empty. Also, we can combine two conditions with the &#8220;AND&#8221; (&amp;&amp;) operator in the <code>Where()<\/code> clause.<\/p>\n<p>Moreover, we can save the updated XML tree in a new file by using the <code>XElement.Save()<\/code> method.<\/p>\n<h3>Update an Existing Element\/Attribute<\/h3>\n<p>By updating one or more elements and\/or attributes, we can change an XML tree:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"5\">var student = StudentsXML.Elements(\"Student\")\r\n    .Where(student =&gt; (int)student.Attribute(\"ID\") == 333)\r\n    .FirstOrDefault();\r\n\r\nstudent.Element(\"FirstName\").Value = \"Jimmy\";\r\n<\/pre>\n<p>Here, we change the first name of the student with ID equal to 333.<\/p>\n<h3>Delete an Existing Element\/Attribute<\/h3>\n<p>Finally, we can delete one or more elements and attributes:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">StudentsXML.Elements(\"Student\")\r\n    .Where(student =&gt; (int)student.Attribute(\"ID\") == 333)\r\n    .Remove();\r\n<\/pre>\n<p>Here, we remove the student with an ID equal to 333.<\/p>\n<h2>Advanced Queries with LINQ to XML<\/h2>\n<p>So far, we have seen simple queries that operate on a single XML tree. However, there are cases where we need to join two or more XML trees in order to get more complex results. Moreover, we can group the results or sort them based on the value of a specific element or attribute.<\/p>\n<p>For the advanced query examples we are going to use the following XML:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\" ?&gt;\r\n&lt;University&gt;\r\n  &lt;Students&gt;\r\n    &lt;Student ID=\"111\"&gt;\r\n      &lt;FirstName&gt;John&lt;\/FirstName&gt;\r\n      &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n      &lt;DateOfBirth&gt;2000-10-2&lt;\/DateOfBirth&gt;\r\n      &lt;Semester&gt;1&lt;\/Semester&gt;\r\n      &lt;Major&gt;Computer Science&lt;\/Major&gt;\r\n      &lt;Courses&gt;\r\n        &lt;Course ID=\"CS101\"&gt;\r\n          &lt;Grade&gt;7.3&lt;\/Grade&gt;\r\n        &lt;\/Course&gt;\r\n        &lt;Course ID=\"CS102\"&gt;\r\n          &lt;Grade&gt;6.9&lt;\/Grade&gt;\r\n        &lt;\/Course&gt;\r\n      &lt;\/Courses&gt;\r\n    &lt;\/Student&gt;\r\n    &lt;Student ID=\"222\"&gt;\r\n      &lt;FirstName&gt;Jane&lt;\/FirstName&gt;\r\n      &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n      &lt;DateOfBirth&gt;2001-2-22&lt;\/DateOfBirth&gt;\r\n      &lt;Semester&gt;1&lt;\/Semester&gt;\r\n      &lt;Major&gt;Electrical Engineering&lt;\/Major&gt;\r\n      &lt;Courses&gt;\r\n        &lt;Course ID=\"EE101\"&gt;\r\n          &lt;Grade&gt;5.6&lt;\/Grade&gt;\r\n        &lt;\/Course&gt;\r\n        &lt;Course ID=\"EE102\"&gt;\r\n          &lt;Grade&gt;8.8&lt;\/Grade&gt;\r\n        &lt;\/Course&gt;\r\n      &lt;\/Courses&gt;\r\n    &lt;\/Student&gt;\r\n    &lt;Student ID=\"333\"&gt;\r\n      &lt;FirstName&gt;Jim&lt;\/FirstName&gt;\r\n      &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n      &lt;DateOfBirth&gt;2000-3-12&lt;\/DateOfBirth&gt;\r\n      &lt;Semester&gt;2&lt;\/Semester&gt;\r\n      &lt;Major&gt;Computer Science&lt;\/Major&gt;\r\n      &lt;Courses&gt;\r\n        &lt;Course ID=\"CS102\"&gt;\r\n          &lt;Grade&gt;7.6&lt;\/Grade&gt;\r\n        &lt;\/Course&gt;\r\n        &lt;Course ID=\"CS103\"&gt;\r\n          &lt;Grade&gt;8.2&lt;\/Grade&gt;\r\n        &lt;\/Course&gt;\r\n      &lt;\/Courses&gt;\r\n    &lt;\/Student&gt;\r\n  &lt;\/Students&gt;\r\n  &lt;Courses&gt;\r\n    &lt;Course ID=\"CS101\"&gt;\r\n      &lt;Title&gt;Intro to programming&lt;\/Title&gt;\r\n      &lt;Credits&gt;5&lt;\/Credits&gt;\r\n    &lt;\/Course&gt;  \r\n    &lt;Course ID=\"CS102\"&gt;\r\n      &lt;Title&gt;Discrete Mathematics&lt;\/Title&gt;\r\n      &lt;Credits&gt;4&lt;\/Credits&gt;\r\n    &lt;\/Course&gt;  \r\n    &lt;Course ID=\"CS103\"&gt;\r\n      &lt;Title&gt;Data structures&lt;\/Title&gt;\r\n      &lt;Credits&gt;6&lt;\/Credits&gt;\r\n    &lt;\/Course&gt;  \r\n    &lt;Course ID=\"EE101\"&gt;\r\n      &lt;Title&gt;Electric fields&lt;\/Title&gt;\r\n      &lt;Credits&gt;5&lt;\/Credits&gt;\r\n    &lt;\/Course&gt;  \r\n    &lt;Course ID=\"EE102\"&gt;\r\n      &lt;Title&gt;Electronics&lt;\/Title&gt;\r\n      &lt;Credits&gt;6&lt;\/Credits&gt;\r\n    &lt;\/Course&gt;  \r\n  &lt;\/Courses&gt;\r\n&lt;\/University&gt;\r\n<\/pre>\n<p>We&#8217;ve created a root element with the name <code>University<\/code>. The root element contains two sequences, one with students and one with courses. The <code>Courses<\/code> elements provide information about the title of the course and the credits each course offers.<\/p>\n<h3>Join two XML Trees (and Create Anonymous Objects)<\/h3>\n<p>Let&#8217;s see a query that joins the two sequences:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3,7\">var students = UniversityXML.Elements(\"Students\").Elements(\"Student\")\r\n    .Where(student =&gt; (string)student.Attribute(\"ID\") == \"111\")\r\n    .Select(student =&gt; new\r\n    {\r\n        Name = (string)student.Element(\"FirstName\") + \" \" + (string)student.Element(\"LastName\"),\r\n        Courses = student.Elements(\"Courses\").Elements(\"Course\")\r\n            .Join(UniversityXML.Elements(\"Courses\").Elements(\"Course\"),\r\n                studentCourse =&gt; (string)studentCourse.Attribute(\"ID\"),\r\n                course =&gt; (string)course.Attribute(\"ID\"),\r\n                (studentCourse, course) =&gt; new\r\n                {\r\n                    Id = (string)course.Attribute(\"ID\"),\r\n                    Title = (string)course.Element(\"Title\"),\r\n                    Grade = (decimal)studentCourse.Element(\"Grade\"),\r\n                    Credits = (int)course.Element(\"Credits\")\r\n                })\r\n    });\r\n<\/pre>\n<p>This query first selects the student with an ID equal to 111. Then, it creates a new anonymous object that consists of two properties: <code>Name<\/code> and <code>Courses<\/code>. The <code>Courses<\/code> property is a list that also contains anonymous objects.<\/p>\n<p>Those objects correspond to the courses taken by the student and combine information from the two lists:<\/p>\n<ul>\n<li><code>Id<\/code>, <code>Title<\/code>, and <code>Credits<\/code> are taken from the <code>Classes<\/code> elements and,<\/li>\n<li><code>Grade<\/code> comes from the <code>Student<\/code> element.<\/li>\n<\/ul>\n<p>Let&#8217;s see this join operation with the query syntax form:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"10\">var students = \r\n    from student in UniversityXML.Elements(\"Students\").Elements(\"Student\")\r\n    where (string)student.Attribute(\"ID\") == \"111\"\r\n    select new\r\n    {\r\n        Name = (string)student.Element(\"FirstName\") + \" \" + (string)student.Element(\"LastName\"),\r\n        Courses =\r\n        (\r\n            from studentCourses in student.Elements(\"Courses\").Elements(\"Course\")\r\n            join course in UniversityXML.Elements(\"Courses\").Elements(\"Course\")\r\n            on (string)(studentCourses.Attribute(\"ID\")) equals (string)course.Attribute(\"ID\")\r\n            select new\r\n            {\r\n                Id = (string)course.Attribute(\"ID\"),\r\n                Title = (string)course.Element(\"Title\"),\r\n                Grade = (decimal)studentCourses.Element(\"Grade\"),\r\n                Credits = (int)course.Element(\"Credits\")\r\n             }\r\n         )\r\n     };\r\n<\/pre>\n<h3>Join two XML Trees (and Create Named Class Objects)<\/h3>\n<p>Here, we will modify the previous example, so that it produces an object (or a list of objects) based on a <code>Student<\/code> class (not anonymous objects):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"3,7\">var students = UniversityXML.Elements(\"Students\").Elements(\"Student\")\r\n    .Where(student =&gt; (string)student.Attribute(\"ID\") == \"111\")\r\n    .Select(student =&gt; new Student()\r\n    {\r\n        Name = (string)student.Element(\"FirstName\") + \" \" + (string)student.Element(\"LastName\"),\r\n        Courses = student.Elements(\"Courses\").Elements(\"Course\")\r\n            .Join(UniversityXML.Elements(\"Courses\").Elements(\"Course\"),\r\n                studentCourse =&gt; (string)studentCourse.Attribute(\"ID\"),\r\n                course =&gt; (string)course.Attribute(\"ID\"),\r\n                (studentCourse, course) =&gt; new Course()\r\n                {\r\n                    Id = (string)course.Attribute(\"ID\"),\r\n                    Title = (string)course.Element(\"Title\"),\r\n                    Grade = (decimal)studentCourse.Element(\"Grade\"),\r\n                    Credits = (int)course.Element(\"Credits\")\r\n                })\r\n    });\r\n<\/pre>\n<h3>Join two XML Trees (and Create a New XElement)<\/h3>\n<p>Instead of creating C# objects (anonymous or not), we can create a new <code>XElement<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-highlight=\"5,8\">var newElement = \r\n    new XElement(\"Students\",\r\n        UniversityXML.Elements(\"Students\").Elements(\"Student\")\r\n            .Where(student =&gt; (string)student.Attribute(\"ID\") == \"111\")\r\n            .Select(student =&gt; new XElement(\"Student\",\r\n                new XElement(\"Name\", (string)student.Element(\"FirstName\") + \" \" + (string)student.Element(\"LastName\")),\r\n                new XElement(\"Courses\", student.Elements(\"Courses\").Elements(\"Course\")\r\n                    .Join(UniversityXML.Elements(\"Courses\").Elements(\"Course\"),\r\n                        studentCourse =&gt; (string)studentCourse.Attribute(\"ID\"),\r\n                        course =&gt; (string)course.Attribute(\"ID\"),\r\n                        (studentCourse, course) =&gt; new XElement(\"Course\",\r\n                            new XAttribute(\"Id\", (string)course.Attribute(\"ID\")),\r\n                            new XElement(\"Title\", (string)course.Element(\"Title\")),\r\n                            new XElement(\"Grade\", (decimal)studentCourse.Element(\"Grade\")),\r\n                            new XElement(\"Credits\", (int)course.Element(\"Credits\"))\r\n                        )\r\n                    )\r\n                )\r\n            ))\r\n        );\r\n<\/pre>\n<p>The result of this query is a new <code>XElement<\/code> that contains the combined information from the join:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;Students&gt;\r\n  &lt;Student&gt;\r\n    &lt;Name&gt;John Doe&lt;\/Name&gt;\r\n    &lt;Courses&gt;\r\n      &lt;Course ID=\"CS101\"&gt;\r\n        &lt;Title&gt;Intro to programming&lt;\/Title&gt;\r\n        &lt;Grade&gt;7.3&lt;\/Grade&gt;\r\n        &lt;Credits&gt;5&lt;\/Credits&gt;\r\n      &lt;\/Course&gt;\r\n      &lt;Course ID=\"CS102\"&gt;\r\n        &lt;Title&gt;Discrete Mathematics&lt;\/Title&gt;\r\n        &lt;Grade&gt;6.9&lt;\/Grade&gt;\r\n        &lt;Credits&gt;4&lt;\/Credits&gt;\r\n      &lt;\/Course&gt;\r\n    &lt;\/Courses&gt;\r\n  &lt;\/Student&gt;\r\n&lt;\/Students&gt;<\/pre>\n<h3>Aggregate Functions<\/h3>\n<p>Let&#8217;s see how we can use LINQ&#8217;s aggregate functions to compute sums and counts:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"11,18\">var students = UniversityXML.Elements(\"Students\").Elements(\"Student\")\r\n    .Select(student =&gt; new\r\n    {\r\n        Name = (string)student.Element(\"FirstName\") + \" \" + (string)student.Element(\"LastName\"),\r\n        TotalCredits = student.Elements(\"Courses\").Elements(\"Course\")\r\n            .Join(UniversityXML.Elements(\"Courses\").Elements(\"Course\"),\r\n                studentCourse =&gt; (string)studentCourse.Attribute(\"ID\"),\r\n                course =&gt; (string)course.Attribute(\"ID\"),\r\n                (studentCourse, course) =&gt; (int)course.Element(\"Credits\")\r\n            )\r\n            .Sum(),\r\n        CoursesCount = student.Elements(\"Courses\").Elements(\"Course\")\r\n            .Join(UniversityXML.Elements(\"Courses\").Elements(\"Course\"),\r\n                studentCourse =&gt; (string)studentCourse.Attribute(\"ID\"),\r\n                course =&gt; (string)course.Attribute(\"ID\"),\r\n                (studentCourse, course) =&gt; (string)course.Attribute(\"ID\")\r\n            )\r\n            .Count()\r\n    });\r\n<\/pre>\n<p>This query returns the sum of credits each student has earned. It also returns a count of the courses he has followed.<\/p>\n<h3>Grouping of Results<\/h3>\n<p>By applying the <code>GroupBy()<\/code> method to our query, we can group the results based on a specific element or attribute:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"4\">var students = \r\n    new XElement(\"Semesters\",\r\n        UniversityXML.Elements(\"Students\").Elements(\"Student\")\r\n            .GroupBy(student =&gt; (int)student.Element(\"Semester\"))\r\n            .Select(group =&gt; new XElement(\"Semester\",\r\n                new XAttribute(\"ID\", (int)group.Key),\r\n                group.Select(s =&gt;\r\n                    new XElement(\"Student\",\r\n                        new XElement(\"FirstName\", (string)s.Element(\"FirstName\")),\r\n                        new XElement(\"LastName\", (string)s.Element(\"LastName\"))\r\n                    )\r\n                )\r\n            )));\r\n<\/pre>\n<p>Here, we group by the <code>Semester<\/code> element and we get sequences of students according to their semester:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;Semesters&gt;\r\n  &lt;Semester ID=\"1\"&gt;\r\n    &lt;Student&gt;\r\n      &lt;FirstName&gt;John&lt;\/FirstName&gt;\r\n      &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n    &lt;\/Student&gt;\r\n    &lt;Student&gt;\r\n      &lt;FirstName&gt;Jane&lt;\/FirstName&gt;\r\n      &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n    &lt;\/Student&gt;\r\n  &lt;\/Semester&gt;\r\n  &lt;Semester ID=\"2\"&gt;\r\n    &lt;Student&gt;\r\n      &lt;FirstName&gt;Jim&lt;\/FirstName&gt;\r\n      &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n    &lt;\/Student&gt;\r\n  &lt;\/Semester&gt;\r\n&lt;\/Semesters&gt;<\/pre>\n<h3>Using Namespaces in LINQ to XML<\/h3>\n<p>So far, we have dealt with XML documents that did not contain any namespaces. We can create a new XML tree with a default namespace like this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"1\">XNamespace st = \"http:\/\/www.testuni.edu\/def\";\r\nXElement students =\r\n    new XElement(st + \"Students\",\r\n        new XElement(st + \"Student\",\r\n            new XAttribute(\"ID\", \"111\"),\r\n            new XElement(st + \"FirstName\", \"John\"),\r\n            new XElement(st + \"LastName\", \"Doe\"),\r\n            new XElement(st + \"DateOfBirth\", \"2000-10-2\"),\r\n            new XElement(st + \"Semester\", \"2\"),\r\n            new XElement(st + \"Major\", \"Computer Science\"),\r\n            new XElement(st + \"Courses\",\r\n                new XElement(st + \"Course\",\r\n                    new XAttribute(\"ID\", \"CS103\"),\r\n                    new XElement(st + \"Grade\", \"7.3\")\r\n                ),\r\n                new XElement(st + \"Course\",\r\n                    new XAttribute(\"ID\", \"CS202\"),\r\n                    new XElement(st + \"Grade\", \"6.9\")\r\n                )\r\n            )\r\n        )\r\n    );<\/pre>\n<p>After we create a new <code>XNamespace<\/code> object, we need to add it to every element in the XML tree to get the result:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;Students xmlns=\"http:\/\/www.testuni.edu\/def\"&gt;\r\n  &lt;Student ID=\"111\"&gt;\r\n    &lt;FirstName&gt;John&lt;\/FirstName&gt;\r\n    &lt;LastName&gt;Doe&lt;\/LastName&gt;\r\n    &lt;DateOfBirth&gt;2000-10-2&lt;\/DateOfBirth&gt;\r\n    &lt;Semester&gt;2&lt;\/Semester&gt;\r\n    &lt;Major&gt;Computer Science&lt;\/Major&gt;\r\n    &lt;Courses&gt;\r\n      &lt;Course ID=\"CS103\"&gt;\r\n        &lt;Grade&gt;7.3&lt;\/Grade&gt;\r\n      &lt;\/Course&gt;\r\n      &lt;Course ID=\"CS202\"&gt;\r\n        &lt;Grade&gt;6.9&lt;\/Grade&gt;\r\n      &lt;\/Course&gt;\r\n    &lt;\/Courses&gt;\r\n  &lt;\/Student&gt;\r\n&lt;\/Students&gt;<\/pre>\n<p>We can also provide a namespace prefix, instead of having a default namespace:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"1,4\">XNamespace st = \"http:\/\/www.testuni.edu\/def\";\r\nXElement students =\r\n    new XElement(st + \"Students\",\r\n        new XAttribute(XNamespace.Xmlns + \"st\", \"http:\/\/www.testuni.edu\/def\"),\r\n        new XElement(st + \"Student\",\r\n            new XAttribute(\"ID\", \"111\"),\r\n            new XElement(st + \"FirstName\", \"John\"),\r\n            new XElement(st + \"LastName\", \"Doe\"),\r\n            new XElement(st + \"DateOfBirth\", \"2000-10-2\"),\r\n            new XElement(st + \"Semester\", \"2\"),\r\n            new XElement(st + \"Major\", \"Computer Science\"),\r\n            new XElement(st + \"Courses\",\r\n                new XElement(st + \"Course\",\r\n                    new XAttribute(\"ID\", \"CS103\"),\r\n                    new XElement(st + \"Grade\", \"7.3\")\r\n                ),\r\n                new XElement(st + \"Course\",\r\n                    new XAttribute(\"ID\", \"CS202\"),\r\n                    new XElement(st + \"Grade\", \"6.9\")\r\n                )\r\n            )\r\n        )\r\n    );<\/pre>\n<p>The difference here is that we add an <code>XAttribute<\/code> object (with the prefix &#8216;st:&#8217;) to the Students element:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"xml\">&lt;st:Students xmlns:st=\"http:\/\/www.testuni.edu\/def\"&gt;\r\n  &lt;st:Student ID=\"111\"&gt;\r\n    &lt;st:FirstName&gt;John&lt;\/st:FirstName&gt;\r\n    &lt;st:LastName&gt;Doe&lt;\/st:LastName&gt;\r\n    &lt;st:DateOfBirth&gt;2000-10-2&lt;\/st:DateOfBirth&gt;\r\n    &lt;st:Semester&gt;2&lt;\/st:Semester&gt;\r\n    &lt;st:Major&gt;Computer Science&lt;\/st:Major&gt;\r\n    &lt;st:Courses&gt;\r\n      &lt;st:Course ID=\"CS103\"&gt;\r\n        &lt;st:Grade&gt;7.3&lt;\/st:Grade&gt;\r\n      &lt;\/st:Course&gt;\r\n      &lt;st:Course ID=\"CS202\"&gt;\r\n        &lt;st:Grade&gt;6.9&lt;\/st:Grade&gt;\r\n      &lt;\/st:Course&gt;\r\n    &lt;\/st:Courses&gt;\r\n  &lt;\/st:Student&gt;\r\n&lt;\/st:Students&gt;<\/pre>\n<p>Of course, we can do various things with XML namespaces. For example, we can have more than one namespace with its respective prefixes. Or, we can use the default namespace, in addition to one or more namespace prefixes.<\/p>\n<h2>Conclusion<\/h2>\n<p>LINQ to XML is a great way to handle XML documents. The topic of LINQ to XML is very large though and we have tried to cover the most important aspects. This includes XML document creation, queries, and modification. We&#8217;ve also touched on some advanced cases, where we have more than one XML tree.<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>LINQ to XML is an in-memory XML programming interface that provides LINQ functionality to programmers. Like the Document Object Model (DOM), we can use LINQ to XML to load XML documents into memory. However, this way we can process them more efficiently, using the advanced features of LINQ. Let&#8217;s dive in. Using LINQ to XML [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12,1033],"tags":[10,544,1201,1020],"class_list":["post-64466","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","category-xml","tag-net","tag-linq","tag-linq-to-xml","tag-xml","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>LINQ to XML - Code Maze<\/title>\n<meta name=\"description\" content=\"LINQ to XML is an in-memory XML programming interface that provides LINQ functionality and we&#039;re going to learn it through some examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/linq-to-xml\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"LINQ to XML - Code Maze\" \/>\n<meta property=\"og:description\" content=\"LINQ to XML is an in-memory XML programming interface that provides LINQ functionality and we&#039;re going to learn it through some examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/linq-to-xml\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-04-11T05:50:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-11T06:02:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Code Maze\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Code Maze\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/\"},\"author\":{\"name\":\"Code Maze\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\"},\"headline\":\"LINQ to XML\",\"datePublished\":\"2022-04-11T05:50:12+00:00\",\"dateModified\":\"2022-04-11T06:02:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/\"},\"wordCount\":1265,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\".NET\",\"LINQ\",\"LINQ to XML\",\"XML\"],\"articleSection\":[\"C#\",\"XML\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/linq-to-xml\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/\",\"url\":\"https:\/\/code-maze.com\/linq-to-xml\/\",\"name\":\"LINQ to XML - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2022-04-11T05:50:12+00:00\",\"dateModified\":\"2022-04-11T06:02:27+00:00\",\"description\":\"LINQ to XML is an in-memory XML programming interface that provides LINQ functionality and we're going to learn it through some examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/linq-to-xml\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/linq-to-xml\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"LINQ to XML\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04\",\"name\":\"Code Maze\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png\",\"caption\":\"Code Maze\"},\"description\":\"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.\",\"sameAs\":[\"https:\/\/www.linkedin.com\/company\/codemaze\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog\"],\"url\":\"https:\/\/code-maze.com\/author\/codemazecontributor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"LINQ to XML - Code Maze","description":"LINQ to XML is an in-memory XML programming interface that provides LINQ functionality and we're going to learn it through some examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/linq-to-xml\/","og_locale":"en_US","og_type":"article","og_title":"LINQ to XML - Code Maze","og_description":"LINQ to XML is an in-memory XML programming interface that provides LINQ functionality and we're going to learn it through some examples.","og_url":"https:\/\/code-maze.com\/linq-to-xml\/","og_site_name":"Code Maze","article_published_time":"2022-04-11T05:50:12+00:00","article_modified_time":"2022-04-11T06:02:27+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Code Maze","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Code Maze","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/linq-to-xml\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/linq-to-xml\/"},"author":{"name":"Code Maze","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04"},"headline":"LINQ to XML","datePublished":"2022-04-11T05:50:12+00:00","dateModified":"2022-04-11T06:02:27+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/linq-to-xml\/"},"wordCount":1265,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":[".NET","LINQ","LINQ to XML","XML"],"articleSection":["C#","XML"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/linq-to-xml\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/linq-to-xml\/","url":"https:\/\/code-maze.com\/linq-to-xml\/","name":"LINQ to XML - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2022-04-11T05:50:12+00:00","dateModified":"2022-04-11T06:02:27+00:00","description":"LINQ to XML is an in-memory XML programming interface that provides LINQ functionality and we're going to learn it through some examples.","breadcrumb":{"@id":"https:\/\/code-maze.com\/linq-to-xml\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/linq-to-xml\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/linq-to-xml\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/linq-to-xml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"LINQ to XML"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/09d29b223012c8e94a68ba62861d0b04","name":"Code Maze","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez-150x150.png","caption":"Code Maze"},"description":"This is the standard author on the site. Most articles are published by individual authors, with their profiles, but when several authors have contributed, we publish collectively as a part of this profile.","sameAs":["https:\/\/www.linkedin.com\/company\/codemaze\/","https:\/\/x.com\/https:\/\/twitter.com\/CodeMazeBlog"],"url":"https:\/\/code-maze.com\/author\/codemazecontributor\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/64466","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=64466"}],"version-history":[{"count":4,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/64466\/revisions"}],"predecessor-version":[{"id":68471,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/64466\/revisions\/68471"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=64466"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=64466"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=64466"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}