XmlDocument from LINQ to XML query

Posted by Ben on Stack Overflow See other posts from Stack Overflow or by Ben
Published on 2010-03-12T16:17:05Z Indexed on 2010/03/12 16:17 UTC
Read the original article Hit count: 465

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about linq-to-xml

Related posts about xmldocument