Questions tagged [xelement]

XElement is part of System.Xml.Linq in .NET Framework. This class represents an XML element, the fundamental XML construct.

An XElement has an XName, optionally one or more attributes, and can optionally contain content.

An XElement can contain the following types of content:

  • XElement
  • XComment
  • XProcessingInstruction
  • XText

Reference: XElement Class on MSDN

883 questions
154
votes
14 answers

Best way to get InnerXml of an XElement?

What's the best way to get the contents of the mixed body element in the code below? The element might contain either XHTML or text, but I just want its contents in string form. The XmlElement type has the InnerXml property which is exactly what…
Mike Powell
  • 5,914
  • 4
  • 28
  • 28
83
votes
6 answers

How to use XPath with XElement or LINQ?

Consider the following XML: 200 OK http://bit.ly/b47LVi b47LVi 9EJa3m
Paul Fryer
  • 9,268
  • 14
  • 61
  • 93
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
47
votes
10 answers

Get the XPath to an XElement?

I've got an XElement deep within a document. Given the XElement (and XDocument?), is there an extension method to get its full (i.e. absolute, e.g. /root/item/element/child) XPath? E.g. myXElement.GetXPath()? EDIT: Okay, looks like I overlooked…
core
  • 32,451
  • 45
  • 138
  • 193
39
votes
1 answer

How to convert from string to XElement object

I have a string like this: "Hey" How can I convert this to an XElement Object?
realn
  • 1,732
  • 3
  • 12
  • 20
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
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
30
votes
5 answers

How to get XElement's value and not value of all child-nodes?

Sample xml: test1 test2 If I look for parent.Value where parent is XElement, I get "test1test2". What I am expecting is "". (since there is no text/value for . What property of XElement should I be…
NiTiN
  • 1,022
  • 1
  • 16
  • 25
28
votes
5 answers

Converting XElement into XmlNode

I know there is no direct method of doing it but still.. Can we convert XElement element into XmlNode. Options like InnerText and InnerXml are XmlNode specific. so,if i want to use these options, what can be done to convert XElement into…
Sangram Nandkhile
  • 17,634
  • 19
  • 82
  • 116
23
votes
4 answers

Removing XElements in a foreach loop

So, I have a bug to remove foreach (XElement x in items.Elements("x")) { XElement result = webservice.method(x); if (/*condition based on values in result*/) { x.Remove(); } } The problem is that calling x.Remove() alters…
CaffGeek
  • 21,856
  • 17
  • 100
  • 184
23
votes
3 answers

How to add an existing Xml string into a XElement

How to add an existing Xml string into a XElement? This code var doc = new XDocument( new XElement("results", "...") ); of course produces this <result></result> but I…
r00kie
22
votes
2 answers

Get child elements from XElement

Possible Duplicate: Children of XElement I want to get child elements from XElement using C#. How can this be done?
CRK
  • 607
  • 3
  • 14
  • 24
22
votes
9 answers

parsing XML with ampersand

I have a string which contains XML, I just want to parse it into Xelement, but it has an ampersand. I still have a problem parseing it with HtmlDecode. Any suggestions? string test = "
paradisonoir
  • 2,892
  • 9
  • 30
  • 41
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
1
2 3
58 59