Click here to Skip to main content
15,913,090 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<language>
<record lang=".net">
<record lang="java">
<record lang="php">


i want to the ouput on string temp=".net,java,php"

What I have tried:

<language>
<record lang=".net">
<record lang="java">
<record lang="php">


i want to the ouput on string temp=".net,java,php". i tried to bind it, but not able to come. can anyone pls help me.
Posted
Updated 28-Aug-17 2:16am

1 solution

That XML isn't valid because there are no closing tags, but assuming your actual XML is valid, you can do this:
C#
using System.Xml.Linq;
C#
string xml = @"<language>
<record lang="".net""></record>
<record lang=""java""></record>
<record lang=""php""></record>
</language>";
string temp = string.Join(",", XDocument.Parse(xml).Root.Elements().Select(x => x.Attribute(XName.Get("lang")).Value));


  • XDocument.Parse parses your string into an XDocument
  • .Root takes the root element, <language>
  • .Elements() takes the child elements, the three <record>-tags.
  • .Select(x => x.Attribute(XName.Get("lang")).Value) takes the value of the "lang" attribute for each of these elements. The result is an IEnumerable<string>.
  • string.Join concatenates these strings, with a comma as separator.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 29-Aug-17 0:20am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900