linq-to-xml

How to use a LINQ query to get XElement values when XElements have same name.

I have a piece of xml like the following: <Table> <Record> <Field>Value1_1</Field> <Field>Value1_2</Field> </Record> <Record> <Field>Value2_1</Field> <Field>Value2_2</Field> </Record> </Table> What i would like is a LINQ query that generates an IEnumerable that i can assign as the datasource of a DataGrid. What...

Optimizing XML in C#

Background We have a project that was started in .NET 1.1, moved to .NET 2.0, and recently moved again to .NET 3.5. The project is extremely data-driven and utilizes XML for many of its data files. Some of these XML files are quite large and I would like to take the opportunity I currently have to improve the application's interaction w...

Can I avoid having to use fully-qualified element names in LINQ to XML?

Say I call XElement.Parse() with the following XML string: var xml = XElement.Parse(@" <?xml version="1.0" encoding="UTF-8"?> <AccessControlPolicy xmlns="http://s3.amazonaws.com/doc/2006-03-01/"&gt; <Owner> <ID>7c75442509c41100b6a413b88b523bd6f46554cdbee5b6cbe27bc08cb3f6a865</ID> <DisplayName>me</DisplayName> </...

How can I split an attribute's value within LINQ(-to-XML)?

I've been feeling like start doing some fun stuff with my delicious bookmarks and LINQ to XML, and I was wondering if there's a way to split the tag attribute within LINQ. What I've meant with split the tag within LINQ was generating a collection of strings for each post element, so the expected result would be a generic collection of p...

LINQ - how to convert query syntax to method syntax

I'm fooling around trying to learn the ins an outs of LINQ. I want to convert the following query (which is working correctly) from query syntax to method syntax, but I can't seem to get it right. Can anyone show me the correct way to accomplish that? var logQuery = from entry in xDoc.Descendants("logentry") ...

Building the 'where' clause in a Linq query

In this query, I always want the 'normal' type element. If the _includeX flag is set, I want the 'workspace' type elements, too. Is there a way to write this as one query? Or build the where clause based on _includeX before submitting the query? TIA... if (_includeX) { query = from xElem in doc.Descendants(_xString) le...

Parsing HTML document: Regular expression or LINQ?

Trying to parse an HTML document and extract some elements (any links to text files). The current strategy is to load an HTML document into a string. Then find all instances of links to text files. It could be any file type, but for this question, it's a text file. The end goal is to have an IEnumerable list of string objects. That par...

How to extract data from an online API?

It's a novice question so be kind to me :) How can I consume a php API in ASP.NET? This API returns an XML document. It is also capable of returning JSON. The output is shown below XML <?xml version="1.0" encoding="UTF-8"?> <Address> <Country>US</Country> <City>Seattle</City> <Result>Done</Result> </Addre...

LINQ to XML Newbie: Moving Nodes From One Node To Another

Greetings! I have an XElement object that contains the following: <Root> <SubSections> <SubSection id="A"> <Foo id="1"> <Bar /> <Bar /> <Bar /> </Foo> <Foo id="2"> <Bar /> <Bar /> </Foo> ...

Inserting XElement to surround another XElement with LinqToXml

Let´s say I have the following xml, <Where><BeginsWith>...</BeginsWith></Where> and now I want to "insert" an <And> clause that surrounds the BeginsWith clause so it looks like this afterward, <Where><And><BeginsWith>...</BeginsWith></And></Where> How do I accomplish that with LinqToXml? The Add method where I essentially do wher...

Load nested XML using LiNQ

I recently asked a question regarding how to Save a list with nested elements to XML but now I am trying to write the loader for the class and have run into problems with it. I am attempting to reverse the answer given (thanks Jon). I believe my core LINQ query is ok, it is the recursion I am struggling with. Here is my code so far (fo...

Update elements within Nested Xml

I have Xml which looks like this: <DataMapper xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; <SqlTable /> <Level_01s> <DataParameter> <SqlTable>MY-Table-Name</SqlTable> <Children> <DataParameter> <SqlTable>MY-Table-Name</SqlTable> <Ch...

How to print <?xml version="1.0"?> using XDocument

Is there any way to have an XDocument print the xml version when using the ToString method? Have it output something like this: <?xml version="1.0"?> <!DOCTYPE ELMResponse [ ]> <Response> <Error> ... I have the following: var xdoc = new XDocument(new XDocumentType("Response", null, null, "\n"), ... which will print this which is fine...

Asp.net: handle null in a foreach loop

Hi, in a ASP.NET application (MVC) I have a foreach loop that loops through a structure that may contain or not some element: <% foreach (XElement segnalazione in ((XElement)ViewData["collezioneSegnalazioni"]).Elements("dossier")) { %> <tr> <td><%= Html.Encode(segnalazione.Element("NUM_DOSSIER").Val...

How to save HTML in XML file using Linq to XML?

Hi, I am trying to use Linq to XML to save & retrieve some HTML between an XML file and a windows forms application. When it saves it to the XML file the HTML tags get xml encoded and it isn't saved as straight HTML. Example HTML: <P><FONT color=#004080><U>Sample HTML</U></FONT></P> Saved in XML File: &lt;P&gt;&lt;FONT color=#00408...

What is the best way to make a LINQ-to-XML statement dynamic?

I'm loading XML data into an object with this LINQ statement: var smartFormFields = from smartFormField in xmlDoc.Descendants("smartFormField") select new Models.SmartFormField { IdCode = smartFormField.Element("idCode").Value, Label = smartF...

How can I get this dynamic WHERE statement in my LINQ-to-XML to work?

In this question Jon Skeet offered a very interesting solution to making a LINQ-to-XML statement dynamic, but my knowledge of lambdas and delegates is not yet advanced enough to implement it: I've got it this far, but of course I get the error "smartForm does not exist in the current context": private void LoadWithId(int id) { XDoc...

How to save XML node back into XML file with LINQ-to-XML?

I've got an XML file which I use to create objects, change the objects, then save the objects back into the XML file. What do I have to change in the following code so that it extracts a node from the XML based on the id, replaces that node with the new one, and saves it back into the XML? The following gives me 'System.Xml.Linq.XEleme...

What is the best relative-path solution for developing/deploying WPF apps with local XML stores?

I have a WPF application which saves its data to XML files in a relative-to-code directory called "Data". I get the relative path to the Data directory with these methods: protected string ApplicationPath { get { return System.IO.Path.GetDirectoryName(System.Reflection.Assembly .GetExecutingAssembly().CodeBase); } } prot...

How do you populate a parent child data structure from XML where generic List<> objects are in parent and child structures using one LINQ query?

I have an XML configuration file that contains a list of documents with field maps for each document. I want to use LINQ to XML to populate an internal data structure which represents the documents as a hierarchy of List<> structures. An attribute called Include determines whether the field should be included. Here is a sample of what ...