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

Convert XElement to string

I have a simple XElement object XElement xml = new XElement("XML", new XElement ("TOKEN",Session["Token"]), new XElement("ALL_INCLUSIVE", "0"), new XElement("BEACH", "0"), new XElement("DEST_DEP", ddlDest.SelectedValue.ToString()), …
user236337
42
votes
3 answers

Force XDocument to write to String with UTF-8 encoding

I want to be able to write XML to a String with the declaration and with UTF-8 encoding. This seems mighty tricky to accomplish. I have read around a bit and tried some of the popular answers for this but the they all have issues. My current code…
Chris
  • 26,744
  • 48
  • 193
  • 345
42
votes
2 answers

Understanding Linq To Xml - Descendants return no results

I'm a completly New to Linq2XML as I code to much lines to perform simple things, and in a simple project I wanted to give it a try... I'm with this for 2 hours and nothing I do get's it right :( I'm really, really thinking to go back to…
balexandre
  • 73,608
  • 45
  • 233
  • 342
41
votes
3 answers

Convert XDocument to Stream

How do I convert the XML in an XDocument to a MemoryStream, without saving anything to disk?
mickyjtwin
  • 4,960
  • 13
  • 58
  • 77
41
votes
1 answer

How can I remove empty xmlns attribute from node created by XElement

This is my code: XElement itemsElement = new XElement("Items", string.Empty); //some code parentElement.Add(itemsElement); After that I got this: Parent element hasn't any namespace. What can I do, to get an Items element…
GrzesiekO
  • 1,179
  • 4
  • 21
  • 34
41
votes
4 answers

XDocument.Descendants not returning descendants

TtT23
  • 6,876
  • 34
  • 103
  • 174
38
votes
1 answer

how to add XElement in specific location in XML Document

I want to create an XML document like this: I want to create it from scratch using code and LINQ-to-XML. In the form Load Event I've written this code: private void Form9_Load(object sender, EventArgs e) { doc = new XDocument(new…
Arian
  • 12,793
  • 66
  • 176
  • 300
37
votes
4 answers

get xelement attribute value

I have an XElement that looks like this: How can I use XML to extract the value of the LoginName attribute? I tried the…
LFurness
  • 669
  • 2
  • 10
  • 21
36
votes
3 answers

XElement => Add children nodes at run time

So let's assume this is what i want to achieve: AAAA BBBB XXX TTT OOO PPP
SF Developer
  • 5,244
  • 14
  • 60
  • 106
36
votes
7 answers

How does one test a file to see if it's a valid XML file before loading it with XDocument.Load()?

I'm loading an XML document in my C# application with the following: XDocument xd1 = new XDocument(); xd1 = XDocument.Load(myfile); but before that, I do test to make sure the file exists with: File.Exists(myfile); But... is there an (easy) way to…
adeena
  • 4,027
  • 15
  • 40
  • 52
36
votes
6 answers

How do I use XPath with a default namespace with no prefix?

What is the XPath (in C# API to XDocument.XPathSelectElements(xpath, nsman) if it matters) to query all MyNodes from this document?
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
35
votes
2 answers

How to Get XML Node from XDocument

How to Get an XML Element from XDocument using LINQ ? Suppose I have an XDocument Named XMLDoc which is shown below: 123 ABC 124
Shine S
35
votes
2 answers

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:
Paul Suart
  • 6,505
  • 7
  • 44
  • 65
34
votes
2 answers

How to convert XElement to XDocument

How can I convert XElement into XDocument? Is there some built-in method for this? The only way I can think of is without new XDocument(xelement.ToString()) which will result in creating big strings and then parsing them, thus reducing the…
atikot
  • 4,761
  • 4
  • 26
  • 34
33
votes
3 answers

why does the Xdocument give me a utf16 declaration?

i'm creating a XDocument like this: XDocument doc = new XDocument( new XDeclaration("1.0", "utf-8", "yes")); when i save the document like this (doc.Save(@"c:\tijd\file2.xml");) , i get this:
Michel
  • 23,085
  • 46
  • 152
  • 242