linq-to-xml

Best LINQ-to-XML query to select nodes based on properties of descendant nodes?

I have the following XML document: <?xml version="1.0" encoding="UTF-8"?> <FamilyTree> <Parent name="Ken"> <Child name="Lorna"> <Grandchild name="Andrew"/> <Grandchild name="Brian"/> </Child> <Child name="Mike"> <Grandchild name="Ann"/> <Grandchild name="Beth"/> </Child> </Parent> <Parent na...

Trying to parse XML tree with Linq to XML (C#)

Hi, I would like to reflect the XML tree in my object structure, but I am quite a beginner in LINQ to XML I have an XML with following structure: <questions> <question id="q1"> <number>1</number> <text>some text11</text> <answers> <answer> <v>some text11</v> </answer> <answer> <v>some text11</v> </a...

How do you guard for Null Reference exceptions in Linq To Xml?

<?xml version="1.0" encoding="utf-8" ?> <pages> <page id="56"> <img id="teaser" src="img/teaser_company.png"></img> </page> </pages> I have an xml file that defines additional resources for pages within a cms. What's the best way to guard for Null Reference exceptions when querying this file with LinqToXml? var page = (from...

how to write this query for this xml data in linq to xml?

hi guys, i've been reading through the linq to xml documentation in msdn and some other tutorials and yet i failed to find the right way to do this query as of now :( Basically, i want to loop through each student in the class, for each question, keep a counter of individual skill, i.e. for student 123, the tally should be 001.101.033.0...

linq to XML (C# to VB.net conversion)

What is equivalent of below in VB.net var list = (from x in xd.Descendants("product").Attributes("title") select new { Title= x.Value}).ToList(); VB.net Dim list = (From x In xd.Descendants("product").Attributes("title") _ Select New ( ??? )).ToList() Thanks ...

linq to xml

want to query below xml <courseList filedUnder="Courses"> <category code="4wd" title="4wd Operation"> <product code="lr" title="4wd Bush Driving"> <duration>1 Day</duration> </product> <product code="hr" title="4wd Defensive Driving"> <duration>1 Day</duration> </product> <product code="sr" title="Self-Recovery"> <durati...

linq to xml (c# to vb.net conversion)

What is the VB.net syntax below for? var list = xd.Descendants("product") .Select(element =>new { Title = element.Attribute("title").Value, Duration = element.Element("duration").Value }).ToList(); ...

reading CDATA with linq to xml

Hello Everyone, I have the following xml file and cannot seem to figure out how to get the value in the elements (which are buried in a CDATA). I am trying to use linq to xml for this. If someone knows how you would convert this into a "Product" object (assume we have a product object that has properties with the same names as the ele...

LINQ Refactoring

Hi guys, I refactored my foreach loop from this before: foreach (KeyValuePair[string, string] param in paramsList) { XmlElement mainNode = xmlDoc.CreateElement("parameter"); mainNode.SetAttribute("name", param.Key); mainNode.SetAttribute("value", param.Value); rootNode.AppendChild(mainNo...

How to get elements value with Linq To XML

Using Linq To XML, how can I get the space_id value (720) from the xml below? I am reading this but I think the namespace in the xml is my stumbling block. <r25:spaces xmlns:r25="http://www.collegenet.com/r25" pubdate="2009-05-05T12:18:18-04:00"> <r25:space id="VE1QOjRhMDAyZThhXzFfMWRkNGY4MA==" crc="" status="new"> <r25:space_id>...

ASP.NET MVC: Using LINQ to XML to render (X)HTML

There has been a lot of discussions on View-engines for ASP.NET MVC and some criticisms against the inline "tag-soup" with for-loops and thing like it. The alternative or complement has been to use HTML-helpers, which are just inline method-calls. When I look inside ASP.NET MVC's HTML-helpers today they are using a class called TagBuil...

LINQ to XML - How does it work?

My question is essentially a simple one, though I'm looking for as in-depth an answer possible here: how does LINQ to XML work behind the scenes? Now, I have had a fair amount of experience working with LINQ to XML for various applications, so it's interfaces is nothing strange to me, but I am quite clueless as to how the internals oper...

LinqtoXML parsing RSS content:encoded

I am trying to parse an RSS feed using Linq to XML like so: XNamespace slashNamespace = "http://purl.org/rss/1.0/modules/slash/"; XDocument rssFeed = XDocument.Load(@"http://blog.daimokuchart.com/index.php/feed/"); var posts = from item in rssFeed.Descendants("item") select new RSSData { ...

XML Exception: Invalid Character(s)

I am working on a small project that is receiving XML data in string form from a long running application. I am trying to load this string data into an XDocument (System.Xml.Linq.XDocument), and then from there do some XML Magic and create an xlsx file for a report on the data. On occasion, I receive the data that has invalid XML charac...

LINQ To XML Syntax for XML Element with Attributes

Hello, I'm a bit of a LINQ newbie and I was having some trouble with the following. I'm trying to perform a query using LINQ on an XML file and store the results in a list of DataClass objects that match the XML. I've got an XML file that is defined like this: <NewDataSet> <NewDataTable> <Field>Accepted ASNs</Field> <Va...

Linq to XML to Generate DDLs

We're storing out database definition as XML in the format at the end of this question. The problem I'm having is getting a list of schemas, the tables in those schemas, the columns in those tables (all with their associated information). My current code (included below the sample XML) grabs everything, completely ignoring nesting and, d...

Does XDocument.Save(string filename) resave the whole file or just changes?

Basically if I do Xdoc.Load(filename), do some changes then do Xdoc.Save(filename) does it only save things that changed such as inserted or removed elements, etc, or does it resave everything? Depending on the answer I'm thinking of determining whether my app is going to save per-change or save on explicit save and on exit. Also consi...

Better way to do this LINQ to XML query?

So say I have this XML file: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <Root> <Category Name="Tasties"> <Category Name="Pasta"> <Category Name="Chicken"> <Recipe Name="Chicken and Shrimp Scampi" /> <Recipe Name="Chicken Fettucini Alfredo" /> </Category> <Category Name="Beef"> ...

Is there an easy way to compare if 2 XDocuments are equal ignoring element/attribute order?

Unit testing my serialization code I found one failed because I had attributes listed in a different order (I'm just comparing the XDocument.ToString() values) and while I could fix that, it really doesn't matter to me in what order the elements or attributes appear as long as they're all there with the right name at the right level of h...

Linq-to-XML: query cleanup

New to Linq, trying to query an XDocument. I want elements where a certain attribute is equal to one of two values. Looking for suggestions on how to streamline this query: query = from xElem in doc.Descendants(StringLiterals._streamNodeName) where ((0 == xElem.Attribute(StringLiterals._typeAttributeName).Valu...