linq-to-xml

Linq to XML Get the next node of the same name

Here is my XML <assets> <asset> <metadata Id="ItemType" Value="Image"/> <metadata Id="ItemUri" Value="http://blah.png"/&gt; </asset> <asset> <metadata Id="ItemType" Value="Image"/> <metadata Id="ItemUri" Value="http://blah2.png"/&gt; </asset> </assets> How do I get the 2nd <metadata>'s v...

Which method should be used instead of descendants() in LINQ to XML ?

I am developing asp.net mobile application. I am using XML as a database. I am using the following query for required output. In the XML file I have the collection of MIMIC nodes & inside MIMICS node I have collection of SECTION nodes. One or more more SECTION node contains one or more SECTION nodes ( nested SECTION node) In the SECTION ...

Retrieve All Elements as a list - LINQ to XML

Hello, I am trying to query an XML file. Below query returns the first element in the sequence. Wondering how to get all elements in the sequence as a List. rsltQuest is of type List of XElement. rsltQuest = doc1.Descendants(xmlns + "QUESTION") .Where(t => t.Attribute("ANSWER").Value == "no").ToList()...

Need help with LINQ to XML

All, I'm new to Linq to XML and have a question on how to generate XML, based on the following format prescribed by a vendor, from a List<string> containing the names of nested directories. - Please help. Thanks Vendor format: <eccu> <match:recursive-dirs value="store" > <match:recursive-dirs value="images" > <revalid...

Whats is lambda method in Linq to XML?

Whats is lambda method in Linq to XML? how to use it? what is its main purpose ? in .net ...

XElements by Attribute Name

Hi All, I need to write a Linq query to get XElements based on Attribute Name. The XMl is not a structured one.. The attribute may there at the top of some XML node or any where in the xml ? Well actually it is the word document the xml file document.xml there will be places it uses the r:Id ,Now i need to get all the Elements which c...

Setting a default value when getting XML values via Linq

I’m using Linq to extract values from some XML. Shown below is a simplified example to show the problem I’m having. The following code works fine on the XML shown below. The problem I have is when the “groupBy” section is missing. Because when it’s missing “Data.Element("groupBy").Element("property)” fails because “Data.Element("groupBy"...

LINQ to XML to POCO object

I've got an XML file that I want to turn in to a list of POCO objects. I have the following working code to read the XML and create objects from it. I just want to check this is a good way to do this and I'm not missing any tricks. In particular with regards to the nested Linq query. XDocument xmlDoc = XDocument.Load(path); var q = fr...

VB.NET: Populating a list in Linq

Assume I have the following XML file: <Movies> <Movie ownerName="Ryan"> <Title>The Lord of the Rings Trilogy</Title> <Title>Saving Private Ryan</Title> <Title>etc</Title> </Movie> <Movie ownerName="Rynina"> <Title>Foo</Title> <Title>Bar</Title> </Movie> </Movies> Wh...

Is it possible to create an object instance based on a string value denoting its type?

I'm trying to dynamically create an object of a certain type in a LINQ-to-XML query based on a string within my XML document. I'm used to being able to dynamically create an object of any type in PHP and JavaScript by simply being able to write something like: $obj = new $typeName(); Ideally, I'd like to be able to do something like:...

Write a XDocument out with annotations

Hi, I have a XDocument where I have added some annotations (.AddAnnotation). Now I want to write the document out with these annotations. Both .ToString and .WriteTo will discard annotations so they will not work. Essentially, I need a custom XML writer but the abstract class XmlWriter don't have any notion about annotations on elements...

Linq to XML join

I have one XML file where values are stored in category element separated by comma: <Company> <name>Test</name> <category>Power,Water,Gas</category> </Company> <Company> <name>Test2</name> <category>Water,Gas</category> </Company> In other XML file only one value is stored in category element: <Bills> <name>Test</name> <category>Pow...

XElement over WCF - strange error

One of my WCF service is returning a XElement object. Visual Studio, the client web app, is keeping showing this message Cannot convert source type 'System.Xml.Linq.XElement[Service.Contracts, Version=1.0.0.0, Culture=neutral]' to target type 'System.Xml.Linq.XElemnet[System.Xml.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b7...

LINQ to XML query

I have two xml files. First company.xml: <company active="1"> <name>name1</name> <location>first floor</location> <room>25</room> <category>power,gas,water</category> </company> <company active="1"> <name>name2</name> <location>second floor</location> <room>23</room> <category>water,gas</category> </com...

linq to XML as a gridview datasource

I'm trying to get all the email nodes for the customers in the sample xml and binding it to a grid. I can't seemt to get past the linq query! Sample XML: <group> <customer> <email>[email protected]></email> </customer> <customer> <email>[email protected]</email> </customer> </group> var query = from result in xml.Elements("custo...

How do I model a dynamic XML element in a C# serialization class?

I have an XML document where one of the element nodes can be dynamic, or of any XML structure. I'm having a difficult time modeling the corresponding C# serialization class. For example I have something like this in my C# class: [XmlAnyElement] public XmlNode Value { get; set; } Where XmlNode is System.Xml.XmlNode. A few notes: I...

How do I remove the CDATA tag of an XElement?

I have some code that receives some XML and there is the possibility that a CDATA tag element will be present. A flag is passed into the method that states whether the CDATA tag should be present, if the flag is false, then the CDATA tag should be removed if present, how would I do this without parsing the query.Value? private static vo...

What is a XML null attribute, and how to handle them in Linq-To-XML?

I'm trying to read a XmlReader into a XDocument //GetContentStructureReader() retrieves the reader from an external source XmlReader reader = GetContentStructureReader(); XDocument.Load(reader); I keep getting the following exception with one specific data source: System.ArgumentNullException was unhandled by user code Messag...

LinQ to xml: Xml without start & end tags.

Hi Folks, I know that when using LinQ it is possible to have an element that is not attached to any root document, but is it possible to take that one step further and have xml data that has no root element; essentially a collection of XElements? xml: <XElement1> <someData>data</someData> </Xelement1> <XElement2> <someData>data<...

XML from DataTable using Linq

This code XmlDataDocument xmlDataDocument = new XmlDataDocument(ds); does not work for me, because the node names are derived from the columns' encoded ColumnName property and will look like "last_x20_name", for instance. This I cannot use in the resulting Excel spreadsheet. In order to treat the column names to make them something mo...