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
33
votes
2 answers

get line number for XElement here

XDocument xdoc = XDocument.Load(file); IEnumerable categories = xdoc.Descendants("Category"); foreach (XElement category in categories) { //get line number for element here... }
user407665
33
votes
3 answers

xml error: Non white space characters cannot be added to content

I am trying to open an xmldocument like this: var doc = new XDocument("c:\\temp\\contacts.xml"); var reader = doc.CreateReader(); var namespaceManager = new XmlNamespaceManager(reader.NameTable); namespaceManager.AddNamespace("g",…
user603007
  • 11,416
  • 39
  • 104
  • 168
31
votes
2 answers

How to get attribute in the XDocument object

I have this xml I want my code to able to do something like this if (xdoc.getAttr("first")=="true") Console.Write("first is true"); How do I do this with LINQ…
r4ccoon
  • 3,056
  • 4
  • 26
  • 32
29
votes
2 answers

XDocument.Descendants() versus DescendantNodes()

I've looked at Nodes() vs DescendantNodes() usages? to see the difference between .Nodes() and .DescendantNodes() but what is the difference between: XDocument.Descendants() and XDocument.DescendantNodes()? var xmlDoc =…
Rob P.
  • 14,921
  • 14
  • 73
  • 109
29
votes
5 answers

How can I write xml with a namespace and prefix with XElement?

This may be a beginner xml question, but how can I generate an xml document that looks like the following? test another…
Chris Conway
  • 16,269
  • 23
  • 96
  • 113
28
votes
2 answers

Find Elements by Attribute using XDocument

This query seems to be valid, but I have 0 results. IEnumerable users = (from el in XMLDoc.Elements("Users") where (string)el.Attribute("GUID") == userGUID.ToString() select el); My XML is as…
Ignacio
  • 7,947
  • 15
  • 63
  • 74
28
votes
6 answers

How to convert XmlNode into XElement?

I have an old XmlNode-based code. but the simplest way to solve my current task is to use XElement and LINQ-to-XML. The only problem is that there is no direct or obvious method for converting a XmlNode to a XElement in .NET Framework. So for…
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
27
votes
4 answers

How do you create an indented XML string from an XDocument in c#?

I have an XDocument object and the ToString() method returns XML without any indentation. How do I create a string from this containing indented XML? edit: I'm asking how to create an in memory string rather than writing out to a file. edit: Looks…
JC.
  • 11,561
  • 11
  • 41
  • 50
27
votes
3 answers

This operation would create an incorrectly structured document

I am new to XML and tried the following but I'm getting an exception. Can someone help me? The exception is This operation would create an incorrectly structured document My code: string strPath = Server.MapPath("sample.xml"); XDocument doc; if…
Vivekh
  • 4,141
  • 11
  • 57
  • 102
26
votes
2 answers

How do I set namespace attributes on an XElement

I need to add the following attributes to an XElement: Adding them as an…
George Mauer
  • 117,483
  • 131
  • 382
  • 612
25
votes
2 answers

How to remove xmlns attribute of a node other than root in an XDocument?

Situation I'm using XDocument to try and remove an xmlns="" attribute on the first inner node: So what I want as a result…
MarioDS
  • 12,895
  • 15
  • 65
  • 121
25
votes
6 answers

xml element name with colon

I'm working against a 3rd party xml api. They have defined a required xml structure similar to the following.
nelsonwebs
  • 840
  • 2
  • 10
  • 19
25
votes
4 answers

How can I query an XDocument with a 'path'?

I would like to query an XDocument object for a given path, (e.g. "/path/to/element/I/want") but I don't know how to proceed.
binncheol
  • 1,393
  • 4
  • 22
  • 38
25
votes
3 answers

C# Linq to XML check if element exists

I have an XML document as follows: "+447528349828" "09/06/24 "Stop" "+447528349828"
Goober
  • 13,146
  • 50
  • 126
  • 195
24
votes
4 answers

C# XDocument Load with multiple roots

I have an XML file with no root. I cannot change this. I am trying to parse it, but XDocument.Load won't do it. I have tried to set ConformanceLevel.Fragment, but I still get an exception thrown. Does anyone have a solution to this? I tried with…
Darksody
  • 481
  • 1
  • 4
  • 15