XmlDocument from LINQ to XML query
- by Ben
I am loading an XML document into an XDocument object, doing a query and then returning the data through a web service as an XmlDocument object.
The code below works fine, but it just seems a bit smelly. Is there a cleaner way to take the results of the query and convert back to an XDocument or XmlDocument?
XDocument xd = XDocument.Load(Server.MapPath(accountsXml));
var accounts = from x in xd.Descendants("AccountsData")
where userAccounts.Contains(x.Element("ACCOUNT_REFERENCE").Value)
select x;
XDocument xd2 = new XDocument(
new XDeclaration("1.0", "UTF-8", "yes"),
new XElement("Accounts")
);
foreach (var account in accounts)
xd2.Element("Accounts").Add(account);
return xd2.ToXmlDocument();