linq-to-xml

Literate LINQ-to-XML: Best practice to deserialize attribute/value to structured variable/value pairs.

Hello everyone. I'm wrestling to deserialize the following XML: <?xml version="1.0" encoding="utf-8" ?> <conf name="settings"> <item name="lorem" one="the" two="quick" three="brown" four="fox" five="jumps" six="over" seven="the" eight="lazy" nine="dog" /> <item name="ipsum" ...

XDocument or XElement parsing of XML element containing namespaces

I am try to read the following string, captured from a log4net UdpAppender. <log4net:event logger="TestingTransmitter.Program" timestamp="2009-08-02T17:50:18.928+01:00" level="ERROR" thread="9" domain="TestingTransmitter.vshost.exe" username="domain\user"> <log4net:message>Log entry ...

How can I get the first element after an element with LINQ-to-XML?

With this code I can get the title out of the following XML file: var xml = XElement.Load (@"C:\\test\\smartForm-customersMain.xml"); string title = xml.Element("title").Value; But how do I make it more exact, e.g. "get the first element after the smartForm element, e.g. something like this: //PSEUDO-CODE: string title = xml.Element(...

Why does getting Elements from an XDocument result in null?

Can anyone explain to me why xml1.Element("title") correctly equals "<title>Customers Main333</title>" but xml2.Element("title") surprisingly equals null, i.e. why do I have to get the XML document as an element instead of a document in order to pull elements out of it? var xml1 = XElement.Load(@"C:\\test\\smartForm-customersMain.xml")...

Using LINQ to XML to Process XML in Multiple Namespaces

I'm trying to parse results from the YouTube API. I'm getting the results correctly as a string, but am unable to parse it correctly. I followed suggestions on a previous thread, but am not getting any results. My sample code is: string response = youtubeService.GetSearchResults(search.Term, "published", 1, 50); XDocument xDoc = XD...

LINQ to XML: how do I get only direct descendants of an XElement?

Dim xml = <Root> <Parent id="1"> <Child>Thomas</Child> </Parent> <Parent id="2"> <Child>Tim</Child> <Child>Jamie</Child> </Parent> </Root> Dim parents = xml.Elements In this case, children includes all the Parent elements and all of the...

LINQ to XML: how do I add child elements in a for-each loop?

Dim names() As String = {"one", "two", "three"} Dim xml As XElement = Nothing For Each name In names If xml Is Nothing Then xml = New XElement(name) Else xml.Add(New XElement(name) End If Next The above code will create something like this: <One> <Two /> <Three /> </One> What I need is something like this: ...

XDocument.ToString() drops XML Encoding Tag

Is there any way to get the xml encoding in the toString() Function? Thanks, rAyt Example: xml.Save("myfile.xml"); leads to <?xml version="1.0" encoding="utf-8"?> <Cooperations> <Cooperation> <CooperationId>xxx</CooperationId> <CooperationName>Allianz Konzern</CooperationName> <LogicalCustomers> But tb_output.Text...

XElement default namespace on attributes provides unexpected behaviour

I am having trouble creating an XML document that contains a default namespace and a named namespace, hard to explain easier to just show what I am trying to produce... <Root xmlns="http://www.adventure-works.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:SchemaLocation="http://www.SomeLocatation.Com/MySchemaDoc.xsd"&gt;...

Why does trying to access an attribute in LINQ-to-XML give me an error?

According to my LINQ book, this slightly altered example should work. Why does it tell me "Object reference not set to an instance of an object"? using System; using System.Xml.Linq; namespace TestNoAttribute { class Program { static void Main(string[] args) { XDocument xdoc = new XDocument( ...

xml file reading

How to read an xml file in silverlight using webclient. I have one task, in this the xml file that i stored in my machine will read and the content od the xml file will display on a data grid. how it will be done? ...

How do you transform a Linq query result to XML?

Newbie in the Linq to XML arena... I have a Linq query with results, and I'd like to transform those results into XML. I'm guessing there must be a relatively easy way to do it, but I can't find it... Thanks! ...

Create XML using Linq to XML and arrays

Hi guys, I am using Linq To XML to create XML that is sent to a third party. I am having difficulty understanding how to create the XML using Linq when part of information I want to send in the XML will be dynamic. The dynamic part of the XML is held as a string[,] array. This multi dimensional array holds 2 values. I can 'build' the ...

How do I add multiple Namespace declarations to an XDocument?

I'm using an XDocument to build an Xml document in a known structure. The structure I am trying to build is as follows: <request xmlns:ns4="http://www.example.com/a" xmlns:ns3="http://www.example.com/b" xmlns:ns2="http://www.example.com/c" > <requestId>d78d4056-a831-4c7d-a357-d14402f623fc</requestId> .... </request> Notice th...

LINQ 2 XML: Simple Input, Brain Fart With Solution

So i have this simple XML text: <errors xmlns="http://schemas.google.com/g/2005"&gt; <error> <domain>GData</domain> <code>InvalidRequestUriException</code> <internalReason>You must specify at least one metric</internalReason> </error> </errors> What the simplest way to extract the value of the internalReason element? ...

Is there an XPath equivilent for Linq to XML?

I have been using Linq to XML for a few hours and while it seems lovely and powerful when it comes to loops and complex selections, it doesn't seem so good for situations where I just want to select a single node value which XPath seems to be good at. I may be missing something obvious here but is there a way to use XPath and Linq to XM...

Whats a Good Example to Write XML using VB.net 2008.

Using this example how would I go about updating an XML file using this example: <foo> <n1> <s1></s1> <s2></s2> <s3></s3> </n1> <n1> <s1></s1> <s2></s2> <s3></s3> </n1> </foo> I Can read from it all day long but for the life of me I cannot seem to write it back into that format. ...

Saving Child Nodes Linq to XML

This is a real newbie question, but I am struggling to get this to work. I am trying to save some data into a XML file, but I can't figure out the syntax need to place the elements in the correct location. I would like to be able to save the xml tree that I built in the corresponding sections based on an association between the plan...

How to convert random XML into RSS with linq

I have this XML data dump <?xml version="1.0" encoding="UTF-8"?> <File> <Row> <Column Name="Job Code"></Column> <Column Name="# of Positions"></Column> <Column Name="Justification (Text Only)"/> <Column Name="Reason"></Column> <Column Name="Job Title"></Column> <Column Name="Purpose...

Using xname in Linq-to-xml

I am writing some code to generate an opml file from a list of rss feeds (parsed) on my site. The user will select checkboxes from a datagrid of rss feeds on my site, and when pressing a button, the heavy lifting will happen. Anyway, I have code like this: foreach (var v in list) { XName xname; doc.Element("ch...