LINQ - dynamically converting XPath to Linq to XML
Asked By viswa rao
06-Sep-10 11:17 PM
Based on XPath I have to serach for atag and replace its contents.
Here is my XML:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<creditcard>
<number>19834209</number>
<expiry>02/02/2002</expiry>
</creditcard>
<name id="1">
<first>Mary</first>
<mi>V</mi>
<last>Jones</last>
</name>
<personal>
<dob>01011966</dob>
<gender>male</gender>
</personal>
<signature>asfu90235r789Z&*()%%^</signature>
</root>
The Xpath is handed to me by SQL call. So I don’t know contents/levels in the Xpath. As an example SQL returns me Xpath as root//Signature. I want to be able to get value of the element pointed to by Xpath and replace it with new content. Can someone please tell me how can I do this with Linq?
Indranil Chatterjee replied to viswa rao
Try the following code:
var doc = XDocument.Load("XMLFile1.xml");//XMLFile1.xml contains the xml fragment supplied by you
var element = doc.XPathSelectElement("root//signature");//Here you can supply any Xpath as parameter
element.Value = "myValue";
doc.Save("XMLFile2.xml");
/*
There are multiple methods for using Xpath
If you expect a single element to be returned, use XPathSelectElement
If you expect multiple elements, use XPathSelectElements
If you expect any node, then use XPathEvaluate
There are overloads for each of these which take an IXmlNamespaceResolver in case you have to work with Xml namespaces
*/
// Please note, the xml fragment provided by you has &* as a part of the value for signature element, which was throwing errors. It's prob'ly not a valid value. I had to replace it with some text. Please check this out.

linq? can someone send me a good link where i can start learning linq? LINQ is a set of extensions to the .NET Framework that encompass language-integrated query, set for queries and provides class libraries to take advantage of these capabilities. Featured Information Essential LINQ (Microsoft .NET Development Series) LINQ is one of Microsoft’s most exciting, powerful new development technologies. Essential LINQ is the first LINQ book written by leading members of Microsoft’s LINQ and C# teams. LINQ to XML LINQ to XML was developed with Language-Integrated Query
LINQ LINQ IN .NET Hi, here is a good article for you http: / / www.eggheadcafe.com / tutorials / aspnet / 042c0b06-95f2-4944-9b52-46be6eeb3e7d / linq-executed-in-parallel-plinq.aspx as well here is some good link from where you can learn more about linq http: / / msdn.microsoft.com / en-us / library / bb907622.aspx http: / / weblogs.asp.net / scottgu / archive 2006 / 05 / 14 / Using-LINQ-with-ASP.NET-_2800_Part-1_2900_.aspx http: / / weblogs.asp.net / jalpeshpvadgama / archive / 2008 / 07 / 31 / what-is-linq-linq-developer-resources.aspx Hope this will help you Hi. . LINQ is a uniform programming model for any kind of data access in .Net framework LINQ
sub-query in linq LINQ how to use the SubQuery in LINQ??? To use the sub-Query in LINQ you have to do your LINQ query like below example one var query = from a in db.ONExport let ExportType = (from select b.sFlagValue).SingleOrDefault() select new { a.nONExportID, a.nExportDescription, a.sOutputFileName, ExportType, MultipleFileOutput }; Hello, LINQ QUery from p in db.Purchases where p.Customer.Address.State = = "WA" | | p.Customer = = null it works Thanks it works YOU ARE WELCOME hi, There is no 'In' subquery in LINQ.Use the 'Any' operator to accomplish the same thing.The left-hand-side of the once http: / / stackoverflow.com / questions / 51339 / how-can-you-handle-an-in-sub-query-with-linq-to-sql All the Best Create New Account keywords: sub-query, in, linq description: div
LINQ Hi All Why we use LINQ and what is diffrence between LINQ and SQL? LINQ which I think is one of the most exciting features in Orcas. LINQ makes the concept of querying a first-class programming concept in .NET. The data to be queried can take the form of XML ( LINQ to XML), databases ( LINQ -enabled ADO.NET: LINQ to SQL, LINQ to Dataset and LINQ to Entities) and objects ( LINQ to Objects). LINQ