Questions tagged [linq-to-xml]

LINQ-to-XML provides a SQL-like query experience for navigating, parsing, and aggregating data stored in XML documents.

LINQ-to-XML provides a SQL-like query experience for navigating, parsing, and aggregating data stored in XML documents.

Related Links

5019 questions
23
votes
3 answers

XDocument and Linq returns null if the element has xmlns attribute

Newbie with XDocuments and Linq, please suggest a solution to retrieve the data from a particular tag in the xml string: If I have a XML string from webservice response (I formatted xml for ease):
Sri Reddy
  • 6,832
  • 20
  • 70
  • 112
23
votes
2 answers

How to query an XDocument with LINQ when elements have a colon in their name?

I am trying to use LINQ to XML in an with the XDocument object. How do you query the result element in the example below? SUCCESS PRIMARY
Jim
  • 233
  • 1
  • 2
  • 5
22
votes
2 answers

XmlTextReader vs. XDocument

I'm in the position to parse XML in .NET. Now I have the choice between at least XmlTextReader and XDocument. Are there any comparisons between those two (or any other XML parsers contained in the framework)? Maybe this could help me to decide…
MarkusSchaber
  • 757
  • 1
  • 8
  • 16
22
votes
4 answers

XElement is automatically adding xmlns="" to itself

I am creating a new XDocument from a table. I have to validate the document from an XSD document and it keeps failing because it add the xmlns="" to one of the Elements when it shouldn't. Here's parts of the code that are pertinent. XNamespace…
Zach
  • 329
  • 1
  • 3
  • 8
22
votes
6 answers

What does the "New ... With" syntax do in VB Linq?

What (if any) is the difference between the results of the following two versions of this VB Linq query? ' assume we have an XElement containing employee details defined somewhere else Dim ee = From e In someXML. _ Select New With…
Steve Davies
  • 562
  • 3
  • 7
  • 14
22
votes
3 answers

XDocument to XElement

How do you convert an XDocument to an XElement? I found the following by searching, but it's for converting between XDocument and XmlDocument, not XDocument and XElement. public static XElement ToXElement(this XmlElement xmlelement) { return…
StackOverflowVeryHelpful
  • 2,347
  • 8
  • 34
  • 46
22
votes
3 answers

Linq to Xml VS XmlSerializer VS DataContractSerializer

In my web method, I get an object of some third party C# entity class. The entity class is nothing but the DataContract. This entity class is quite complex and has properties of various types, some properties are collections too. Of course, those…
21
votes
2 answers

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…
Davy8
  • 30,868
  • 25
  • 115
  • 173
21
votes
1 answer

Creating XDocument with xsi:schemaLocation namespace

I need to create the following XML and I'm trying to do this using XDocument. However, I'm having trouble specifying the name spaces.
Robin Day
  • 100,552
  • 23
  • 116
  • 167
21
votes
4 answers

How to prevent System.Xml.XmlException: Invalid character in the given encoding

I have a Windows desktop app written in C# that loops through a bunch of XML files stored on disk and created by a 3rd party program. Most all the files are loaded and processed successfully by the LINQ code that follows this statement: XDocument…
John Adams
  • 4,773
  • 25
  • 91
  • 131
21
votes
3 answers

Performance: XDocument versus XmlDocument

I have read a comparison between the two here. This is primarily a question of performance, relating to both memory and speed. I've got several XML documents that are upwards of 100 - 300 K in size. I've noticed that there is some lag when loading…
CodeMonkey1313
  • 15,717
  • 17
  • 76
  • 109
21
votes
6 answers

Linq to XML - update/alter the nodes of an XML Document

I've got 2 Questions: 1. I've sarted working around with Linq to XML and i'm wondering if it is possible to change an XML document via Linq. I mean, is there someting like XDocument xmlDoc = XDocument.Load("sample.xml"); update item in…
knox
21
votes
2 answers

How do I add an XElement to a document, avoiding the "incorrectly structured document" error?

// Remove element with ID of 1 var userIds = from user in document.Descendants("Id") where user.Value == "1" select user; userIds.Remove(); SaveAndDisplay(document); …
Finglas
  • 15,518
  • 10
  • 56
  • 89
21
votes
2 answers

Regex in Linq statement?

I'm writing a short C# to parse a given XML file. But 1 of the tag values can change, but always includes words "Fast Start up" (disregarding case and spaces, but needs to be in the same order) in the where clause. I'm not sure how I would do this…
jerryh91
  • 1,777
  • 10
  • 46
  • 77
20
votes
9 answers

' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1

I am trying to read a xml file from the web and parse it out using XDocument. It normally works fine but sometimes it gives me this error for day: **' ', hexadecimal value 0x1F, is an invalid character. Line 1, position 1** I have tried some…
Nghia Nguyen
  • 2,585
  • 4
  • 25
  • 38