-2

I have a list which is of type ListItem

The ListItem is a custom class I made and it looks like following:

public class ItemList
{
    public string ItemID { get; set; }
    public int QuantitySold { get; set; }
    public string GalleryURL { get; set; }
}

I need to store this List into a folder on my project which looks like this:

/Helpers/CachedResults/FileGoesHere

And so that I can reconstruct that list each time whenever I need it back in code...

Jim
  • 2,974
  • 2
  • 19
  • 29
User987
  • 3,663
  • 15
  • 54
  • 115
  • 1
    Perhaps XML serialization? http://stackoverflow.com/questions/4123590/serialize-an-object-to-xml – ispiro Nov 20 '16 at 20:20
  • This could be it, could you show me a practical example with my case ? :) – User987 Nov 20 '16 at 20:27
  • Guys I don't want to serialize it to XML... the code before the list is built I actually converted the XML file into this list.. Now I need to just store it into some kind of file so that I can rebuild it when needed... – User987 Nov 20 '16 at 20:28
  • Maybe bytes array or something like that?? Something simple and easy so that I can rebuild that file back to this original list :D – User987 Nov 20 '16 at 20:29
  • Well, you can serialize your list to xml and unserialize it back to your list. – Kinetic Nov 20 '16 at 20:31
  • @Kinetic Yeah I can see that from the link... But once I serialize it to XML, how do I store it and read from that Serialized XML file to rebuild it? I haven't seen that on the link u supplied – User987 Nov 20 '16 at 20:31
  • deserialise back to xml, you should make your question clear? – Vivek Nuna Nov 20 '16 at 20:32
  • read that file using code – Vivek Nuna Nov 20 '16 at 20:33
  • How to unserialize object from XML : stackoverflow.com/questions/10518372/… – Kinetic 11 secs ago edit – Kinetic Nov 20 '16 at 20:33
  • @viveknuna can u show me an example what you mean by that ? :) – User987 Nov 20 '16 at 20:33
  • @Kinetic that's a lot of code... there has to be a simpler way than that :/ – User987 Nov 20 '16 at 20:34
  • A lot of code? Are you kidding me? – Kinetic Nov 20 '16 at 20:34
  • u should follow the link provided by @Kinetic – Vivek Nuna Nov 20 '16 at 20:34
  • You need about three lines of code for serialization and about the same for unserialization. – Kinetic Nov 20 '16 at 20:35
  • @Kinetic it's way more than just 3 lines... It is 3 lines when you setup and map all the classes... The XML response that I get from ebay is ugly and I should re-do every bit piece of code that I did to reconstruct the XML to do this.. It's just not the way to go with this one... – User987 Nov 20 '16 at 20:42
  • 1
    I fail to see the problem. – Kinetic Nov 20 '16 at 20:46

2 Answers2

3

I would do this with LINQ to XML.

To use it, you need to add a reference to System.Xml.Linq (in VS, right-click References -> Add Reference -> check System.Xml.Linq).

You don't need to convert the List into an array, but of course, you can, if you want. That's done simply with the list.ToArray() method.

The writing-to-file code would be:

List<ListItem> list = new List<ListItem>();

// (the list is populated)

XDocument xDocument = new XDocument(); // create empty document
XElement root = new XElement("list"); // add root element, XML requires one single root element
xDocument.Add(root); // add root to document
foreach(var listItem in list) 
{
    var xElement = new XElement("list-item", // <list-item />
        new XAttribute("id", listItem.ItemID), // id="id"
        new XAttribute("quantity-sold", listItem.QuantitySold), // quantity-sold=5
        new XAttribute("gallery-url", listItem.GalleryURL) // gallery-url="foo/bar"
    );
    root.Add(xElement); // add list-item element to root
}

xDocument.Save("Helpers/CachedResults/File.xml"); // save file

Note that the directory must exist for Save to work!

Then parsing:

List<ListItem> list = new List<ListItem>();

XDocument xDocument = XDocument.Load("Helpers/CachedResults/File.xml"); // load from file
XElement root = xDocument.Element("list"); // get root element

foreach (XElement xElement in root.Elements("list-item")) // traverse through elements inside root element
{
    list.Add(new ListItem // add list items
    {
        ItemID = xElement.Attribute("id").Value, // parse id
        QuantitySold = Int32.Parse(xElement.Attribute("quantity-sold").Value), // parse int for QuantitySold
        GalleryURL = xElement.Attribute("gallery-url").Value // parse url
    });
}

And we're done!

mkkekkonen
  • 1,704
  • 2
  • 18
  • 35
  • It's basically manual XML serialization and unserialization. You'll get the same result. A perfectly good answer. – Kinetic Nov 20 '16 at 20:48
  • Well you did write the code nicely, I gotta tell you that... But this isn't the way to go since I actually just before thist List is created, I did exactly parse the XML document that I got from ebay and converted it to this list of items :D... This would be a step-back or redoing everything I actually did XD – User987 Nov 20 '16 at 20:49
  • Why would you need to redo your "ebay code" though? – Kinetic Nov 20 '16 at 20:50
  • Here's the thing, I get the response from ebay for specific items (ID's) that I pass through the XML request to ebay... ebay returns me the results in the form of XML file. I convert the XML responses I got from ebay into this List and now I'm trying to save that list onto the file system so that I can re-use it to display the result to the end user (sorta way of caching the results)... – User987 Nov 20 '16 at 20:52
  • Sounds ok to me, but I still fail to see what you would need to recode in this process. – Kinetic Nov 20 '16 at 20:54
0

According to your comment:

Now I need to just store it into some kind of file so that I can rebuild it when needed...

you just need the following lines:

File.WriteAllText(path, xml);
xml = File.ReadAllText(path);

where xml is the string you get from serializing / the serialized string that you stored previously and want to deserialize)

(This is besides the actual serialization that I mentioned in my comment: Serialize an object to XML)

EDIT

From the comments, it seems like your trying to parse xml that you receive. Then perhaps you should consider using the classes that were made for that. See Parsing XML using XDocument .

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291