I'm working on a tool, and I'm using an XML configuration file, that I need to add configuration items to programmatically. I'm using .Net Core, and I've been using this question as a guide for my work, but it appears that the part with below works, but doesn't have a member function as listed in the example.
IEnumerable<XElement> rows = root.Descendants("Student");
Here is the Sample code from the original question that I'm attempting to use for my own purposes:
XDocument xDocument = XDocument.Load("Test.xml");
XElement root= xDocument.Element("School");
IEnumerable<XElement> rows = root.Descendants("Student");
XElement firstRow= rows.First();
firstRow.AddBeforeSelf(
new XElement("Student",
new XElement("FirstName", firstName),
new XElement("LastName", lastName)));
xDocument.Save("Test.xml");
The original sample code used an IEnumerable < XElement > object that is supposed to have a .first, and I'm hoping has a .last function that returns the first entry in the document. The issue is Visual Studio doesn't allow me to use either, and this is a partial of the function I'm writing:
XDocument xD = XDocument.Load(_config);
XElement root = xD.Element("Store");
List<XElement> rows = root.Descendants("template");
XElement[] arr = rows.ToArray();
arr[arr.Length - 1].AddAfterSelf(
new XElement("template"),
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc));
I changed over to a List<> of XElement and I can get the last node that way, and append after, but there's still the issue that the root.Descendants() call wants to return the IEnumerable<> only.
Here are my using Statements:
using System;
using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Linq;