0

I want to convert XMl string to generic list.

My XML code :

<Color>
<t_options optionImage="1593-Black.png" optionid="4625050"  RowId=1 />
<t_options optionImage="1593-Red.png" optionid="4625051"  RowId=2 />
<t_options optionImage="1593-Blue.png" optionid="4625052"  RowId=3 />
<t_options optionImage="1593-Green.png" optionid="4625053"  RowId=4 />
</Color>
Jeff Yates
  • 61,417
  • 20
  • 137
  • 189
user2354274
  • 187
  • 5
  • 16
  • 1
    possible duplicate of [Is it possible to deserialize XML into List?](http://stackoverflow.com/questions/608110/is-it-possible-to-deserialize-xml-into-listt) – DerApe Aug 05 '15 at 14:23
  • 1
    Yea, that's nice that you want to do that... You should have provided some info how you want your list to look like... Meaning which data it should contain. However: use your brain and build a function that reads the XML and creates a list of it. We're here to solve problems, not to do other people's work. – Tobias Knauss Aug 05 '15 at 14:25
  • I have class like this `Public class t_options { public string optionImage {get;set;} public string optionid {get;set;} public string RowId {get;set;} }` I am expecting List listoption – user2354274 Aug 05 '15 at 14:29

2 Answers2

1

Start with System.Xml.Linq; You'll want to load your xml file and then parse your document. For example,

var doc = XDocument.Load("file.xml");
IEnumerable<XElement> elements = doc.Descendants(tagNameHere);

And if you want to create a list, you can access those elements by doing something like this:

List<string> myElements = new List<string>();
XElement element = elements.ElementAt(0);
myElements.Add(element.Value);

This is just to get you started. I suggest you read here:

https://msdn.microsoft.com/en-us/library/system.xml.linq(v=vs.110).aspx

and do some more research on parsing xml files.

cyi
  • 96
  • 4
0

I would use XmlSerializer, since you already have a well defined class you want to use. You just have to encapsulate the List part so that the Serializer will know what to do about the <Color> tag:

public class t_option
{
    [XmlAttribute]
    public string optionImage { get; set; } 

    [XmlAttribute]
    public string optionid { get; set; } 

    [XmlAttribute]
    public string RowId { get; set; }
}

public class Color
{
    public Color()
    {
        t_options = new List<t_option>();
    }

    [XmlElement("t_options")]
    public List<t_option> t_options {get; set;} 
}

public static void Main(string[] args)
{
    string xml = @"<Color>
  <t_options optionImage='1593-Black.png' optionid='4625050'  RowId='1' />
  <t_options optionImage='1593-Red.png' optionid='4625051'  RowId='2' />
  <t_options optionImage='1593-Blue.png' optionid='4625052'  RowId='3' />
  <t_options optionImage='1593-Green.png' optionid='4625053'  RowId='4' />
</Color>";

    XmlSerializer xser = new XmlSerializer(typeof(Color));

    using (XmlReader xr = XmlReader.Create(new StringReader(xml)))
    {
        xr.MoveToContent();
        Color c = (Color)xser.Deserialize(xr);
        Console.WriteLine(c.t_options.Count);
    }
    //   Console.WriteLine(l.Count);
    Console.ReadKey();
}

Note that your XML had to be corrected - attribute values must be in quotes.

Dan Field
  • 20,885
  • 5
  • 55
  • 71
  • 1
    I strongly discourage tying serialization structure to architecture structure like this. Tight coupling should be avoided unless absolutely necessary. – Jeff Yates Aug 05 '15 at 15:17
  • He doesn't necessarily have to use the Color class in any architectural way except to get the XML into a POCO. Once he has it in that he can do whatever he wants to the list. I think this method is a bit easier to work with than parsing everything out from an `XDocument` - especially since he ultimately wants it in a POCO (`t_options`) – Dan Field Aug 05 '15 at 15:42