linq-to-xml

What the equivalent LINQ to XML be for the following code?

What is the equivalent Linq to XML for the following code: public List<listing> GetList() { List<listing> listings = new List<listing>(); if(File.Exists(this.XmlFilePath)) { DataSet ds = new DataSet(); ds.ReadXml(this.XmlFilePath); DataTable dt = ds.Tables["listing"]; ...

Explain the LINQ to XML for a beginner below?

Can someone explain the LINQ to XML below? Also, what is the correct way to check if the method returned a List with Data? Do you just check if the List is empty. Code: public List<Listing> GetList() { if (File.Exists(this.xmlFilePath)) { XDocument doc = XDocument.Load(this.xmlFilePath); var listings = from r...

How do I optimize a Linq to Xml query againist attributes?

Given the following Xml fragment: <root> <sheetData> <row r="1" /> <row r="2" /> <row r="3" /> <row r="4" /> <row r="5" /> <row r="6" /> <row r="7" /> </sheetData> </root> Which can be created with the following code: XElement testElement = new XElement("root", new XElement("sheetData", new...

LINQ over XML: find the top-level parent

I'm using C# and LINQ to traverse my XDocument. Lets say I have XML like this: <Root> <Element ID="1"> <Element ID="2"> <Element ID="3" /> ... </Element> <Element ID="50"> ... </Element> </Element> <Element ID="x"> ... </Element> </Root> Now let's say I have the ID 3, and manage to find t...

How can I add a new property in an already existing xml file?

Hi, I am working on an application that parses a .csproj file. It needs to add an additional value to the <NoWarn> property if it exists. In the event that the property does not exist I want the application to add this property with its value to the specified parent node. How can I achieve this? I am using LINQ-to-XML to parse the proj...

LINQ to read XML (C#)

I got this XML file <root> <level1 name="A"> <level2 name="A1" /> <level2 name="A2" /> </level1> <level1 name="B"> <level2 name="B1" /> <level2 name="B2" /> </level1> <level1 name="C" /> </root> Could someone give me a C# code using LINQ, the simplest way to print this result: (Note the extra space if it is a level2 node)...

How to replace xml content in conditionally selected elements using linq (VB 2008)

I'm working on a .NET application (VB 2008) that gets all of its data from a web-based Django application. I'm using Linq-to-XML and Linq-to-objects in the app. One API call is giving me XML data in the following format that I am loading into an XDocument. <django-objects version="1.0"> <object pk="1" model="app.person"> <...

Perserving Array from AnonymousType

I looked online for some references but didn't have too much luck. Hopefully it's just some simple thing I'm overlooking, but in my code I'm looping through the participant list and storing the queried results into the array. As you can tell, my foreach statement will just add the last element of the array since the first one is replaced...

Concatenate collection of XML tags to string with LINQ

I'm stuck with using a web service I have no control over and am trying to parse the XML returned by that service into a standard object. A portion of the XML structure looks like this <NO> <L>Some text here </L> <L>Some additional text here </L> <L>Still more text here </L> </NO> In the end, I want to end up with one String...

Help returning EDMX Elements

I have a Entity Framework EDMX file and I want to traverse the EntityType elements as well as the child Property elements but I can't figure out how to do it. It always returns an empty set of descendants. Any help appreciated ...

return all xml items with same name in linq

Hi All how can I query an xml file where I have multiple items with the same name, so that I can get all items back. Currently I only get the first result back. I managed to get it to work with the following code, but this returns all items where the specific search criteria is met. What I want as output is to get two results back where...

linq question: querying nested collections

I have a Question class that has public List property that can contain several Answers. I have a question repository which is responsible for reading the questions and its answers from an xml file. So I have a collection of Questions (List) with each Question object having a collection of Answers and I'd like to query this collection o...

Inserting an element into xml produces an unnecessary propertry xmlns=

Whenever I insert an element into my xml document, the system adds an xmlns="" attribute to it. How do i get rid of it? Why is it there? Im using very simple linqtoxml. I've got a simple XML file (Note there is no Xml declaration line and it contains a namespace): <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/deve...

Reading inner list only with LinqToXml

With LinqToXml I would like to read out the album list for a given artist from an xml file like <?xml version="1.0" encoding="utf-8" ?> <artists> <artist name="Beatles"> <albums> <album title="Please Please Me" year="1963"/> <album title="With the Beatles" year="1963"/> ... </albums> </artist> ... I tr...

Populate XDocument from String

I'm working on a little something and I am trying to figure out whether I can load an XDocument from a string. XDocument.Load() seems to take the string passed to it as a path to a physical XML file. I want to try and bypass the step of first having to create the physical XML file and jump straight to populating the XDocument. Any idea...

Linq Question: Combined Where Clauses

Grettings! I have some XML that looks like this: <Root> <SectionA> <Item id="111"> <Options> <Option val="a" cat="zzz"> <Package value="apple" /> <Feature value="avacado" /> </Option> <Option val="b" cat="yyy"> ...

Jaxb equivalent in C#

Using JAXB in Java it is easy to generate from a xml schema file a set of Java classes that xml conforming to that schema can be deserialized to. Is there some C# equivalent of JAXB? I know that Linq can serialize and deserialize classes to/from xml files. But how can I generate C# classes from xml schema file and then use this classes ...

Import XML to SQL using C#

I know its kind non sportiness asking for this kind of help, But I am relay stacked on this for while, Temporally I am reading two C# books and working everyday over 9 hours. Okay here is my problem I have WIN C# application that I almost done. In SQL i got a three tables look like this: CREATE TABLE [dbo].[Racuni]( [BROJ] [varchar](1...

How do I retrieve data with Linq to XML?

I need to find ShippingMethod and the attribute Code and Destination from the following piece of XML: <ScoreRule> <ShippingMethod Code="UPS1DA"> <Destination Country="US" Area="IL" Value="0" /> </ShippingMethod> </ScoreRule> How do I retrieve that data with Linq to XML? ...

LinqToXML: Getting elements with given value

I have this xml-file: <objects> <object> <value>test</value> </object> <object> <value>foo</value> </object> <object> <value>bar</value> </object> </objects> Now, I want to query this xml, and retrieve all the object-elements where the text in the value-element = "foo" Is there a easy way of doing thi...