string xml = "<ABCProperties> <Action> Yes | No | None </Action><Content>
<Header> Header Text </Header><Body1> Body Paragraph 1 </Body1>
<BodyN> Body Paragraph N</BodyN></Content><IsTrue> true | false </IsTrue>
<Duration> Long | Short </Duration></ABCProperties>";
Here, from the XML, I want to extract certain strings. First is the Header Text in the Header tags.
When, I try
XDocument doc = XDocument.Parse(xml);
var a = doc.Descendants("Header").Single();
I get variable a = <Header> Header Text </Header>
. How can I get only var a = Header Text
?
Secondly, I want to get text of all the Body paragrahs. It can be either Body1, Body2 or BodyN. How can I get contents of all Body tags.
Can anyone help me with this?
You're asking for the Header
element - so that's what it gives you. If you only want the text of that, you can just use:
var headerText = doc.Descendants("Header").Single().Value;
To find all the body tags, just use a Where
clause:
var bodyText = doc.Descendants()
.Where(x => x.Name.LocalName.StartsWith("Body"))
.Select(x => x.Value);
See more on this question at Stackoverflow