linq-to-xml

LINQ subquery with AND operator displays no results

Me again... I have an XML File that contains different categories which I would like to query for different attributes. <item> <title>Industries</title> <category type="Channel">Automotive</category> <category type="Type">Cars</category> <category type="Token">Article</category> <category t...

How would I use LINQ2XML in this scenario?

I have my LINQ2XML query working half way to my goal: var XMLDoc = XDocument.Load("WeatherData.xml"); var maximums = from tempvalue in XMLDoc.Descendants("temperature").Elements("value") where tempvalue.Parent.Attribute("type").Value == "maximum" select (string)tempvalue; var minimums ...

better way to fill a dropdownlist with linq to xml

Hi, I have a bunch of dropdownlists which I populate from an xml file using linq. I've tried the following to populate them and it somehow works, the only problem is that when an element is missing e.g. subtitle I get an empty space in the dropdownlist. Also I don't get to have the distinct values of each dropdownlist but instead all ...

What is wrong with my simple query?

I'm new to Linq to Xml. I have a very simple xml file like this: <Items> <Item> <Stuff>Strings</Stuff> </Item> <Item> <Stuff>Strings</Stuff> </Item> </Items> And I'm trying to query it like this: XDocument doc = XDocument.Load(myStream) from node in doc.Descendants(XName.Get("Item")) select new { Stu...

Using Linq-to-XML to insert, with threading

What is the best way to ensure thread-safe Linq-to-XML, for writing? We recently got overloaded on our web cluster and I had to pull a quick overloaded.aspx out of my butt to capture emails that people can be contacted with later when the site becomes more responsive. In my short 5 minute haste, I wrote this: private static object Loc...

Linq2XML, Why isn't Element(), Elements() working?

I am trying to traverse through a simple sitemap (adding and deleting elements on the fly.) This is the sample layout <?xml version="1.0" encoding="UTF-8"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schem...

query xmlnode using linq

I have following file: <root> <Product desc="Household"> <Product1 desc="Cheap"> <Producta desc="Cheap Item 1" category="Cooking" /> <Productb desc="Cheap Item 2" category="Gardening" /> </Product1> <Product2 desc="Costly"> <Producta desc="Costly Item 1" category="Decoration"/> <Productb des...

read rss items with linq

Hi, I have the following rss feed which I would like to query to get all the items with a specific category named subchannel. So far I only managed to get to the first element, but only if it is the first in the list. How would I write the linq query to filter the rss feed to only show items where a certain "subchannel" matches a specif...

xml, linq parsing

I am modifying my xml using LINQ Dim feedXML As XDocument = XDocument.Parse(m_xmld.OuterXml.ToString()) Dim SortedFields = From field In feedXML.Descendants("fields") Dim sFieldList = From field In SortedFields.Descendants("field") Order By Integer.Parse(field.@position) what I am trying to do is sort my "fields" in an ascending o...

select two attributes in a node using LINQ

I have the following node: <NodeA desc="Cheap Item 1" category="Cooking" /> I selected the 'category' attribute using the following: .Where(attr => attr.Name == "category") .Select(attr => attr.Value); How can I select both the 'desc' and 'category' now ...

How to select a specific node with LINQ-to-XML

I can select the first customer node and change its company name with the code below. But how do I select customer node where ID=2? XDocument xmldoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("These are all the customers transfered from the database."), new XElement("Customers", ...

XDocument.Descendants() not returning any elements

I'm trying to bind a Silverlight DataGrid to the results of a WCF service call. I was not seeing the data displayed in the grid, so when I ran through the debugger, I notice that XDocument.Descendants() was not returning any elements even when I was passing in a valid element name. Here is the XML that is passed back from the service: <...

How do I find an XML element by attribute using LINQ to XML?

I'm learning LINQ to XML and need to find the existence of an element with a particular attribute. At the moment I'm using: XElement groupCollectionXml = XElement.Parse(groupCollection.Xml); IEnumerable<XElement> groupFind = from vw in groupCollectionXml.Elements("Group") where (string) vw.Attribute("Name") == groupName sele...

Test to see if an xelement exists

I'm reading an XML file that looks like this: <?xml version="1.0" encoding="utf-8"?> <VehicleList> <Vehicle> <Item> <Name>F-150</Name> <Maker>Ford</Maker> <Color>Black</Color> <Price>30000</Price> </Item> <ItemSpecific> <NameValueList> <Name>Mileage</Name> <Value>56000</Value> </NameValueList> <NameVa...

Delete XElement based on attribute

Hi, I'm still playing around with xml. Now I have a file that looks like this: <?xml version="1.0" encoding="utf-8"?> <Attributes> <AttributeSet id="10110"> <Attribute id="1">some text here</Attribute> <Attribute id="2">some text here</Attribute> <!-- 298 more Attribute nodes follow --> <!-- note that the value for the id att...

Auto Deserialize XML into a class collection within Silverlight?

Sorry if this is generic in nature, but I have a question that maybe is related to my lack of understanding of some core underlying rules of .NET and Silverlight. I have a basic project at the moment that simply: An ASP.NET generic handler writes out XML Within Silverlight, I am using the WebClient object to get the XML output, in fac...

Need help with an Opposite to Inner join Query using LINQ

Hi, I have two tables in an XML Dataset. T1, T2. Each of the tables has a ID column. T1 has a list of Customers T2 has a list of Orders I want to build a LINQ query that returns only the ID of the customers that do not have orders. In other words customer ID's that do not exist in the T2 table. Oh yea, I'm using C# Thanks! ...

Handling null references when using eg Linq-To-Xml

Is there a better/shorter way to handle (lots of) null references, for example when I'm using LinqToXML. I wrote this extention for XElement that handles it quite nicely, but maybe there is another way? And what about the function name? "And" isn't really descriptive. public static class XmlExtentions { public static T And<T>(th...

Find or Create Element in LINQ-to-XML

I want to set the value/children of an element that may or may not already exist. If the element doesn't exist, I want to have it automagically created for me. This way, my code only has to worry about the contents of the element... not whether or not it already exists. (By the time I'm done with it, it's guaranteed to exist). Does th...

How to best detect encoding in XML file?

To load XML files with arbitrary encoding I have the following code: Encoding encoding; using (var reader = new XmlTextReader(filepath)) { reader.MoveToContent(); encoding = reader.Encoding; } var settings = new XmlReaderSettings { NameTable = new NameTable() }; var xmlns = new XmlNamespaceManager(settings.NameTable); var context =...