Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,i have a xml string.i want to convert the xml string to a List.so that i can fetch all the data of c1,c2, ...cn.

if the c1 ,c2 .... columns are fixed ,then easy to convert a list through a class object.
but here it is not fixed.please help me.below is my xml file.
C#
<root>

<columns>

    <column>
        <c1>100</c1>
        <c2>200</c2>
        <cn>300</cn>
    </column>

    <column>
        <c1>111</c1>
        <c2>222</c2>
        <cn>333</cn>
    </column>

    <column>
        <c1>MAX Newsletter</c1>
        <c2>OLS Application</c2>
        <cn>Total funded accounts</cn>
    </column>

</columns>


</root>
Posted
Updated 25-Sep-13 20:45pm
v2
Comments
So, what code have you tried, which is not solving the purpose?
Ehsan Sajjad 22-Sep-15 2:55am    
use anonymous type here or create a column type according to your requirement

Hi,

This will help you in achieving your goal. Try this out.

XmlDocument doc = new XmlDocument();
     doc.Load("D:\\testpp.xml");
     ArrayList list = new ArrayList();
     XmlNode idNodes = doc.SelectSingleNode("columns/column");
     foreach (XmlNode node1 in idNodes.ChildNodes)
         list.Add(node1.InnerText);


In this you can able to get all the subsequent items listed in the list. It will work irrespective of your number of elements as you have mentioned.

If you face any issues please let me know.

Thanks,
Sisir Patro
 
Share this answer
 
Comments
Member 12671588 18-Aug-16 10:52am    
what is "columns/column"
[no name] 12-Oct-16 7:17am    
"columns" is the parent element and "column" is the child item in the given structure. Hope you have followed the given structure in the question.
Vandana87 26-Sep-17 6:12am    
hi, what if i have multiple columns with different name like below sample -


<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://apple.com/itunes/importer" version="film5.2">
<provider>applemusic
<language>en-US


<type>film
<subtype>feature
<country>US
<title>Process
<studio_release_title>Process
<synopsis>Directed by Khalil Joseph....
<production_company>Pulse Films

<ratings>
<rating system="ag-movies" reason="Parental Guidance" code="PG"/>
<rating system="au-oflc" reason="General" code="G"/>
<rating system="br-movies" reason="Ominous theme" code="10"/>
<rating system="de-fsk" reason="Approved for age 6 and older" code="6+"/>
<rating system="hk-movies" reason="Neither obscene nor indecent" code="ALL"/>
<rating system="in-movies" reason="12 years of age" code="UA"/>
<rating system="nz-oflc" reason="Anyone can be shown or sold this" code="G"/>
<rating system="zw-movies" reason="Parental Guidance" code="PG"/>


<crew>
<crew_member billing="top">
<display_name>Kahlil Joseph
<roles>
<role>Director


<crew_member billing="top">
<display_name>Kahlil Joseph
<roles>
<role>Screenwriter


<!-- Additional crew members here -->
[no name] 16-Jan-19 7:11am    
Always you can populate the parent element as a list and operate on it like nested element.
Member 13935631 9-Jan-19 1:10am    
what if you have multiple xml files(actual file itself) that you would want to put in a list? how would you put each xml into list then bind it to view
Try this. Please remove the root element from the string
C#
[XmlRoot(ElementName = "column")]
   public class Row
   {
       public string C1 { get; set; }
       public string C2 { get; set; }
       public string Cn { get; set; }
   }
 const string xmlString = @"<columns><column><c1>100</c1><c2>200</c2><cn>300</cn></column><column><c1>111</c1><c2>222</c2><cn>333</cn></column> <column>  <c1>MAX Newsletter</c1><c2>OLS Application</c2>  <cn>Total funded accounts</cn> </column></columns>";
           XDocument doc = XDocument.Parse(xmlString);
           if (doc.Root != null)
           {
               List<Row> items = (from r in doc.Root.Elements("column")
                            select new Row
                                       {
                                           C1 = (string)r.Element ("c1"),
                                           C2 = (string)r.Element("c2"),
                                           Cn = (string)r.Element("cn")
                                       }).ToList();
           }


Hope this helps
 
Share this answer
 
v2
Comments
connect2manas 26-Sep-13 4:00am    
thanks for your answer.
But there is no fix fields like c1,c2,cn .so that we can not define that.
Jameel VM 26-Sep-13 4:58am    
do you have more than three column?

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