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
83
votes
7 answers

Search XDocument using LINQ without knowing the namespace

Is there a way to search an XDocument without knowing the namespace? I have a process that logs all SOAP requests and encrypts the sensitive data. I want to find any elements based on name. Something like, give me all elements where the name is…
Mike Barlow - BarDev
  • 11,087
  • 17
  • 62
  • 83
69
votes
6 answers

How to print using XDocument

Is there any way to have an XDocument print the xml version when using the ToString method? Have it output something like this: ... I have the following: var xdoc = new…
JD Frias
  • 4,418
  • 3
  • 21
  • 24
69
votes
3 answers

Children of XElement

How do I get just the children of an XElement? I am currently using the XElement.Descendants() function, which returns all levels of XElements, rather than just the child nodes. What I would really like is an IEnumerable of just the children.
Superman
  • 3,686
  • 6
  • 34
  • 46
66
votes
4 answers

How to change XML Attribute

How can I change an attribute of an element in an XML file, using C#?
Mike
61
votes
7 answers

Finding element in XDocument?

I have a simple XML greatest Band 1 2 regular…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
61
votes
5 answers

Uses for the '"' entity in HTML

I am revising some XHTML files authored by another party. As part of this effort, I am doing some bulk editing via Linq to XML. I've just noticed that some of the original source XHTML files contain the " HTML entity in text nodes within those…
DavidRR
  • 18,291
  • 25
  • 109
  • 191
61
votes
4 answers

Use Linq to Xml with Xml namespaces

I have this code : /*string theXml = @"
Tim
  • 2,887
  • 10
  • 33
  • 33
55
votes
4 answers

XDocument: saving XML to file without BOM

I'm generating an utf-8 XML file using XDocument. XDocument xml_document = new XDocument( new XDeclaration("1.0", "utf-8", null), new XElement(ROOT_NAME, new…
systempuntoout
  • 71,966
  • 47
  • 171
  • 241
53
votes
2 answers

C# Distinct on IEnumerable with custom IEqualityComparer

Here's what I'm trying to do. I'm querying an XML file using LINQ to XML, which gives me an IEnumerable object, where T is my "Village" class, filled with the results of this query. Some results are duplicated, so I would like to perform a…
Fueled
  • 8,776
  • 9
  • 29
  • 31
52
votes
3 answers

How do I make the value of an XElement be wrapped in ![CDATA[***]]?

This is when using XDocument from .net. I thought this might work... xElement.Element(elementName).Value = new XCData(value).ToString(); ... but it comes out like this... <![CDATA[hello world]]>
Ian Warburton
  • 15,170
  • 23
  • 107
  • 189
52
votes
4 answers

linq question: querying nested collections

I have a Question class that has public List property that can contain several Answers. I have a question repository which is responsible for reading the questions and its answers from an xml file. So I have a collection of Questions (List) with…
kitsune
  • 11,516
  • 13
  • 57
  • 78
51
votes
2 answers

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…
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
47
votes
4 answers

How do I get a NameTable from an XDocument?

How do I get a NameTable from an XDocument? It doesn't seem to have the NameTable property that XmlDocument has. EDIT: Judging by the lack of an answer I'm guessing that I may be missing the point. I am doing XPath queries against an XDocument like…
Simon Keep
  • 9,886
  • 9
  • 63
  • 78
47
votes
3 answers

XDocument containing namespaces

I have the following XML which I am trying to query with XDocument:
Gineer
  • 2,358
  • 4
  • 26
  • 40
44
votes
5 answers

Use XDocument as the source for XmlSerializer.Deserialize?

I would like to invoke XmlSerializer.Deserialize passing it an XDocument. It can take a Stream, an XmlReader or a TextReader. Can I generate one of the above from XDocument without actually dumping the XDocument into some intermediate store, such as…
Roman Starkov
  • 59,298
  • 38
  • 251
  • 324